capturing values after an optional slash - regex

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

Related

Get n chars after last / in url path [duplicate]

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.

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

Regex ending character can be one of 2

I have a url that I am trying to get the id out of. Problem is, the url could look like this "https://www.website.com/blah/1234567890:0", "https://www.website.com/blah/1234567890/" or "https://www.website.com/blah/1234567890"
It is now the first option that is giving me trouble. Basically, all I want is "1234567890", so for the last option, I need to omit the :0.
Here is what I tried when capturing id with/without ending /:
([^/]*)\/?$
Here is what I tried to cover both with/without ending / and the :0, but it does not work as I thought (id is match 6, but I've no way of knowing it will always be 6):
([^/]*)[/:?$]
Note your [/:?$] subpattern matches a single char, either /, or :, ? or $ (the $ symbol inside [...] is not a special regex operator any longer).
You may make the first negated character class lazily quantified, and add an optional group that would match / or :0 one or zero times:
([^\/]*?)(?:\/|:0)?$
See the regex demo. Replace 0 with [0-9] to match any digit at the end of the string.
Details:
([^\/]*?) - Group 1: zero or more chars other than / as few as possible (due to the *? quantifier)
(?:\/|:0)? - an optional non-capturing group matching one of the two alternatives, 1 or 0 times: / or :0
$ - end of string.

Puppet dynamic variable from hostname

I am looking at trying to get a dynamic variable out of my ec2's hostname. Hostnames follow this pattern
us-east-1b-application-type-environment-138-10.domain.com
I would like my variable to end up looking like this
application-type-environment
Using this
$variable = regsubst($hostname, '/[a-z]{1}[0-9]{1}-([^-]+)-[0-9]{1,3}/', '')
I end up with this though
us-east-1b-application-type-environment-138-10
How can I get my expected outcome?
You do not need regex delimiters in regsubst. You need to match the whole string to be able to remove it and only keep what you need. The techique consists in matching what you do not want to keep and matching and capturing what you do want to have asa result.
You can use
regsubst($hostname, '^[^0-9]*[0-9][a-z]-(.*?)-[0-9]{1,3}.*$', '\1')
I think you are trying to get just what is in between the first [digit][lowercase-letter] chunk and a three digit chunk.
Here is a regex demo
Breakdown of the expression:
^ - start of line (if start of string is meant, replace with \A)
[^0-9]* - 0 or more non-digit symbols (all but digits, this can be replaced with \D*)
[0-9][a-z]- - a digit followed by a lowercase letter followed by - (the same as \d[a-z])
(.*?) - match and capture any characters but a newline as few as possible before the closest...
-[0-9]{1,3} - 1 to 3 digits (the same as \d{1,3})
.*$ - 0 or more any characters but a newline up to the end of line (if end of string is meant, replace with \z).

Regex for 5 digit number with optional characters

I am trying to create a regex to validate a field where the user can enter a 5 digit number with the option of adding a / followed by 3 letters. I have tried quite a few variations of the following code:
^(\d{5})+?([/]+[A-Z]{1,3})?
But I just can't seem to get what I want.
For instance l would like the user to either enter a 5 digit number such as 12345 with the option of adding a forward slash followed by any 3 letters such as 12345/WFE.
You probably want:
^\d{5}(?:/[A-Z]{3})?$
You might have to escape that forward slash depending on your regex flavor.
Explanation:
^ - start of string anchor
\d{5} - 5 digits
(?:/[A-Z]{3}) - non-capturing group consisting of a literal / followed by 3 uppercase letters (depending on your needs you could consider making this a capturing group by removing the ?:).
? - 0 or 1 of what precedes (in this case that's the non-capturing group directly above).
$ - end of string anchor
All in all, the regex looks like this:
You can use this regex
/^\d{5}(?:\/[a-zA-Z]{3})?$/
^\d{5}(?:/[A-Z]{3})?$
Here it is in practice (this is a great site to test your regexes):
http://regexr.com?36h9m
^(\d{5})(\/[A-Z]{3})?
Tested in rubular