This question already has answers here:
Regex get nth value separated with slash
(2 answers)
Closed last month.
I have a regex pattern to use on working directory paths. The objective attempt is to grab everything before four forward slashes. Therefore, never exceed capturing string values after four forward slashes.
I have attempted two approaches:
[^\\].*[\\]
Which grabs all values up to a forward slash , for example:
C:\Users\testing\again\later
#I will grab
C:\Users\testing\again\
However, if there is a leading forward slash, this will capture it, regardless if it occurs four times or not. I have also attempted:
(?=[\\]){4}.*[\\]
However, this again will grab for any number of leading forward slashes.
^(?:[^\\]*\\){4}
Will grab everything up to the 4th slash.
^ - Matches start of line
(?: ) - Non-capturing group:
[^\\]* - Matches any number (including zero) of characters except backslashes
\\ - Matches literal backslash
{4} - Repeats non-Capturing group 4 times
(.+)(?:\\.*){4}
Will grab everything before the four backslashes. In your case C:
Related
This question already has answers here:
Get 5 Characters After Last Slash
(4 answers)
Closed 5 days ago.
I want to match 10 characters after / in urls but when I've extras / It's not able to ignore it can someone help me here how to strictly select last / with 10 chars.
([a-z0-9]+)(?:\/?$)
Using this regex I'm able to get last part but I only want 10 chars.
([a-z0-9]{10})(?:\/?$)
Using this I'm getting last 10 chars but I need first 10 chars. Also I want to ignore last / if there is no path after it.
Example
https://www.facebook.com/reel/1a1c6e99h60a3169h86816
https://www.facebook.com/reel/0e2c4a1a1c6e6990eac186/
Output
1a1c6e99h6
0e2c4a1a1c
I need first 10 chars
Ok, good.
So ask for them.
(online)
We anchor against a / slash so we get the beginning of a path element.
\/([a-z0-9]{10})
If you wanted anywhere between eight a dozen letters, then [a-z]{8,12} would work.
A slight improvement:
(?<!\/)\/([^\/]){10}(?!.*\/.+)
Explained:
(?<!\/): make sure there is not a preceding slash
\/: Slash
([^\/]){10}: Match 10 non slashes
(?!.*\/.+) make sure there is not another slash with chars after.
You could try this pattern: (?:\/)([a-z0-9]{10})(?:[^\/]*\/?$)
Your expected output should be captured in matched group 1.
This pattern will only match 10 characters of last string after \, instead matched all groups of 10 characters after a \, for example in this case:
https://www.facebook.com/reel/g012345678/g123456789/0e2c4a1a1c6e6990eac186/
You can use
.*\/(.{10})
This reads, "match zero or more characters, as many as possible, followed by a forward slash, followed by 10 characters that are saved to capture group 1". The contents of capture group 1 contains the desired string of 10 characters.
Capture group 1 will contain the 10 characters following the last forward slash that is followed by at least 10 characters. Here are three examples. The contents of capture group 1 is indicated by the position of the 10-segment centipede below.
abc/1234567890123/1234567890123
^^^^^^^^^^
abc/1234567890123/1234567
^^^^^^^^^^
abc/1234567/1234567
^^^^^^^^^^
abc/1234/1234
Demo.
.* is greedy, meaning that it will consume as many characters as possible, including forward slashes, so long as the rest of the regular expression is satisfied.
In the first example the last forward slash is followed by 10 characters, so those 10 characters are saved to capture group 1.
In the second example the last forward slash is followed by fewer than 10 characters so the 10 characters following the next-to-last forward slash are captured.
The third example is the same as the second, except the 10 characters captured includes a forward slash.
In the fourth example no forward slash is followed by 10 characters so no match is made.
I cannot be sure that the behaviour of this regex in the three cases other than the first is what the OP wants because the question does not speak to those situations.
I am trying to write in regex a string that allows me to have
an alphanumeric string of length no longer than 5 (as an example) [a-z0-9]{3,5}
followed by an optional forward slash /?
that cannot end in a 3
I want to capture any group of at least 3, with our without a slash, and then anything after it.
And I am having a very hard time accomplishing this. If I require the slash / it is much easier to do so.
When I try
(?=.+\/?.+)[a-z0-9]{2,5}\/?(?<!3\/|3)
I can capture what I want - up until the slash, but can't crack how to get anything after IF legit things occur
(?=.+\/?.+)[a-z0-9]{2,62}\/?.?
My requirement for length goes up by 1 - to 4 instead of 3 - due to the additional . I put after the \/?. I could change my match to account for it, but it becomes really difficult.
(?=.+\/?.+)[a-z0-9]{2,5}\/?(?<!3\/|3)$
This only gives me the last slash or non slash follwed by 2,5 characters.
(?=.+\/?.+)[a-z0-9]{2,62}\/?.*
or
(?=.+\/?.+)[a-z0-9]{2,62}\/?.?+
simply then ignores my ending rule, of not being able to close with3/ or 3. Also this allows me to use more than 5 characters before the slash. Def not what I want :)
Is there a way to make an optional field still maintain length and ending rules?
I am running this script on both regexr.com and https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_regexp and gitbash and not getting the results I would like
Try:
^[a-z0-9]{3,5}(?<!3)(?:$|\/.*)
Regex demo.
^ - beginning of the string
[a-z0-9]{3,5} - capture a-z0-9 between 3 and 5 times
(?<!3) - the last character should not be 3
(?:$|\/.*) - match either end of string $ or / and any number of characters.
If the last character in this range [a-z0-9] should not be a 3 you can exclude it like [a-z124-9]
^[a-z0-9]{2,4}[a-z124-9](?:\/.*)?$
Explanation
^ Start of string
[a-z0-9]{2,4} Match 2-4 chars in the ranges a-z 0-9
[a-z124-9] Match a single char a-z and then either 1,2 4-9
(?:\/.*)? Optionally match / and the rest of the line
$ End of string
See a regex101 demo.
If you can not match a 3 at all:
^[a-z124-9]{3,5}(?:\/.*)?$
See another regex101 demo
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I'm trying to develop a regex with the following rules:
it should accept solely numbers,
if the string contains any letters or any other special characters, the whole string should be rejected,
regarding spaces, there should only be one consecutive number group, which can be surrounded by spaces,
if there are more than one consecutive number group, with spaces in between the groups, that whole string should be rejected.
Example Cases:
accepted:
1234
[SPACE][SPACE]111[SPACE]
[SPACE]111[SPACE][SPACE]
declined:
1a234
aa1234aa
1234a
12#4
[SPACE]11[SPACE]111
[SPACE]11[SPACE]111#
So far, I've come up with this ([0-9]+[^\s]*) which can be seen here.
What modifications do I have to do to achieve the scenario I want above?
Use this:
^\s*\d+\s*$
All we need to do is accept one or more digits bounded by zero or more spaces on either side.
EDIT:
Just add a capturing group around the digits to use them later:
^\s*(\d+)\s*$
Demo
The pattern you tried ([0-9]+[^\s]*) matches 1+ digits and 0+ times a non whitespace character using a negated character class [^\s]* matching any character except a whitespace char (So it would match aa)
It can match multiple times in the same string as there are no anchors asserting the start ^ and the end $ of the string.
If you want to match spaces, instead of matching \s which could also match newlines, you could match a single space and repeat that 0+ times on the left and on the right side.
^ *[0-9]+ *$
Regex demo
If you only need the digits, you could use a capturing group
^ *([0-9]+) *$
Regex demo
^\s*[0-9]+\s*$
notice that I've used [0-9] instead of \d
[0-9] will accept only Arabic number (Western Arabic Number)
\d may accept all form of digit in unicode like Eastern Arabic Number, Thai,...etc like (١,٢,٣, ๑,๒,๓, ...etc) at least this is the case in XSD regex when its validate XML file.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I need a regular expression to validate strings with the prefix 'CON' followed by an optional space followed by 8 digits.
I've tried various expressions, I got tangled up and now I'm lost.
^(CON+s\?d{8})$
\bCON\b\S?D{8}
Syntax is off a bit
^(CON\s?\d{8})
( starts a capturing group
CON is exactly matched
\s matches any white space character and the ? makes it optional
\d{8} matches 8 digits
) ends the capturing group
You were pretty well off to start, Hope this helps :)
keeping in mind If there is no space, then there shouldn't be 8 more digits
^CON(\ \d{8})?
If the string you are looking for can be part of a larger string (note that in this case it may be preceded or followed by anything, even other digits):
CON\s?\d{8}
If the string must match in full, use ^$ to designate that:
^CON\s?\d{8}$
You can add variations to it, if say you want it to begin/end with a word boundary - use \bto indicate that. If you want it to end in a non-digit, use \D+ at the end, instead of $.
Finally, if you want the string to end with an EOL or a non-digit, you may use an expression like this:
CON\s?\d{8}(\D+|$) or the same with a non-capturing group: CON\s?\d{8}(?:\D+|$)
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