Finding character no number between two slashes regex - regex

I have the following regex
(?<=[\/]).*(?=[\/]) I'm trying to run on FM7-4/E27/U20 and I'm trying to only get the character between the two slashes, no numbers. I tried adding [^0-9] but wasn't able to get a match. Any help would be appreciated.

You can use
(?<=\/)[^\/\d]*(?=\d*\/)
See the regex demo.
Details:
(?<=\/) - the / char must appear immediately on the left
[^\/\d]* - zero or more chars other than / and digits
(?=\d*\/) - a positive lookahead that requires zero or more digits and then / immediately on the right.

Related

Regex should find substring that does not start with /

I want to find version numbers in a long string.
Example: "Hellothisisastring 12.3 blabla"
I need to find a substring that is a version number but does not start with "/".
Example: "Hellothisisastring /12.3 blabla"
shouldn't match.
I already build following regex:
[0-9]+.[0-9]
How can I detect a that the version number does not start with "/". The problem is that it is not at the beginning of the string. I already tried with negative lookahead.
(?!/)[0-9]+.[0-9] still matches with a slash before.
Thanks for any help :)
You need to use a lookbehind and include a digit pattern to also fail the positions right after digits:
(?<![\d\/])[0-9]+\.[0-9]+
See the regex demo.
Also, you may match any amount of . + digits using
(?<![\d\/])[0-9]+(?:\.[0-9]+)+
See this regex demo. Details:
(?<![\d\/]) - a negative lookbehind that fails the match if there is a digit or / immediately to the left of the current location
[0-9]+ - one or more digits
(?:\.[0-9]+)+ - one or more sequences of a . and one or more digits.

Capture number if string contains "X", but limit match (cannot use groups)

I need to extract numbers like 2.268 out of strings that contain the word output:
Approxmiate output size of output: 2.268 kilobytes
But ignore it in strings that don't:
some entirely different string: 2.268 kilobytes
This regex:
(?:output.+?)([\d\.]+)
Gives me a match with 1 group, with the group being 2.268 for the target string. But since I'm not using a programming language but rather CloudWatch Log Insights, I need a way to only match the number itself without using groups.
I could use a positive lookbehind ?<= in order to not consume the string at all, but then I don't know how to throw away size of output: without using .+, which positive lookbehind doesn't allow.
With your shown samples, please try following regex.
output:\D+\K\d(?:\.\d+)?
Online demo for above regex
Explanation: Adding detailed explanation for above.
output:\D+ ##Matching output colon followed by non-digits(1 or more occurrences)
\K ##\K to forget previous matched values to make sure we get only further matched values in this expression.
\d(?:\.\d+)? ##Matching digit followed by optional dot digits.
Since you are using PCRE, you can use
output.*?\K\d[\d.]*
See the regex demo. This matches
output - a fixed string
.*? - any zero or more chars other than line break chars, as few as possible
\K - match reset operator that removes all text matched so far from the overall match memory buffer
\d - a digit
[\d.]* - zero or more digits or periods.

Regex lookahead part of group accepted

I'm using regex in powershell 5.1.
I need it to detect groups of numbers, but ignore groups followed or preceeded by /, so from this it should detect only 9876.
[regex]::matches('9876 1234/56',‘(?<!/)([0-9]{1,}(?!(\/[0-9])))’).value
As it is now, the result is:
9876
123
6
More examples: "13 17 10/20" should only match 13 and 17.
Tried using something like (?!(\/([0-9]{1,}))), but it did not help.
You may use
\b(?<!/)[0-9]+\b(?!/[0-9])
See the regex demo
Alternatively, if the numbers can be glued to text:
(?<![/0-9])[0-9]+(?!/?[0-9])
See this regex demo.
The first pattern is based on word boundaries \b that make sure there are no letters, digits and _ right before and after an expected match. The second one just makes sure there are no digits and / on both ends of the match.
Details
(?<![/0-9]) - a negative lookbehind making sure there is no digit or / immediately to the left of the current location
[0-9]+ - one or more digis
(?!/?[0-9]) - a negative lookahead making sure there is no optional / followed with a digit immediately to the right of the current location.

Regex Ignore First 6 Matches Of Character

This is my string: /my/name/is/the/following/string/name.lastname/file.txt
I want to extract name.lastname from this string.
I've tried using \/.*\.app, but this selects:
/my/name/is/the/following/string/name.lastname
How can I ignore the first 6 or 7 /'s?
You have quite a few good answers going for you. Here's one that uses positive look ahead (?=), with the end of string $.
([^\/]+)(?=\/[^\/]+$)
The benefit here is you can have as many folders prior to your last folder, and it will still work.
DEMO
If we break this down, you have a
capturing group: ([^\/]+), and a
positive look ahead (?=\/[^\/]+$).
The capturing group will match everything except ^ a forward slash /, one to as many times possible +. This would actually capture every string between a forward slash, so that's why we use the positive lookahead.
The biggest factor in your positive lookahead is that it looks for the end of your string $ (signified by the dollar sign). It will look for everything after a forward slash / (hence the (?=\/ portion), then it will ensure no other forward slashes exists but match all other characters [^\/] one to unlimited times + to the end of the string $.
You may use a repeating pattern to consume, but not match, the first six components of the path:
(?:\/[^\/]+){6}\/([^\/]+)
Your item will be available in the first capture group.
Demo
If you want a more flexible solution, i.e. the string between
last 2 slashes (not necessarily 6th and 7th), you can use:
\/([^\/]+)\/(?!.*\/)
Meaning:
\/ - A slash.
([^\/]+) - Capturing group No 1 - a sequence of chars other than a slash.
This is what you actually want to match.
\/ - Another slash.
(?! - Negative lookahead for:
.*\/ - a sequence of any chars and a slash.
) - End of negative lookahead (works even in JavaScript version of Regex).
The above negative lookahead actually means: Nowhere further
can occur any slash.
try this ,it will match 6 or 7 th position
([a-z\.]*)(?=\/[a-z]*\.txt)
(?=\/[a-z]*\.txt) to check ends with .txt
([a-z\.]*) CapturingGroup to capture the name
Demo
((\/)[a-b]*).[^\/]{12}
Hi, Please try the above Reg ex, it should return what you expecting

Exclude the last character of a regex match

I have the following regex:
%(?:\\.|[^%\\ ])*%([,;\\\s])
That works great but obviously it also highlights the next character to the last %.
I was wondering how could I exclude it from the regex?
For instance, if I have:
The files under users\%username%\desktop\ are:
It will highlight %username%\ but I just want %username%. On the other hand, if I leave the regex like this:
%(?:\\.|[^%\\ ])*%
...then it will match this pattern that I don't want to:
%example1%example2%example3
Any idea how to exclude the last character in the match through a regex?
%(?:\\.|[^%\\ ])*%(?=[,;\\\s])
^^
Use a lookahead.What you need here is 0 width assertion which does not capture anything.
You can use a more effecient regex than you are currently using. When alternation is used together with a quantifier, there is unnecessary backtracking involved.
If the strings you have are short, it is OK to use. However, if they can be a bit longer, you may need to "unroll" the expression.
Here is how it is done:
%[^"\\%]*(?:\\.[^"\\%]*)*%
Regex breakdown:
% - initial percentage sign
[^"\\%]* - start of the unrolled pattern: 0 or more characters other than a double quote, backslash and percentage sign
(?:\\.[^"\\%]*)* - 0 or more sequences of...
\\. - a literal backslash followed by any character other than a newline
[^"\\%]* - 0 or more characters other than a double quote, backslash and percentage sign
% - trailing percentage sign
See this demo - 6 steps vs. 30 steps with your %(?:\\.|[^" %\d\\])*%.