I'm trying to match 10 chars after last backslash / in a URL.
Example input https://www.facebook.com/reel/1a1c6e99h60a3169h86816
Output should be https://www.facebook.com/reel/1a1c6e99h
Example input https://www.facebook.com/reel/1a1c6e99h60a3169h86816/
Output should be https://www.facebook.com/reel/1a1c6e99h
Used this regex ([^/]*$) to select everything after last /
But this way getting last path & truncating it later will be overhead.
It's better if I can select URL from starting to and of last / with 10 chars.
This is what I've tried regexr.com
Lead with .*:
.*/.{10}
See live demo.
The .* is greedy, so the matched / will be the last slash. Slashes at the end aren't matched because there aren't 10 chars after it.
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
I have troubles matching text between a word pages and 6th slash which occurs before HomeGardenerTools.
I want to transform this to:
https://facebook.com/pages/category/Home---Garden-Store/HomeGardenerTools
To this:
https://facebook.com/pg/HomeGardenerTools
What I have thus far is start of selection:
pages(.?)
and matching anything before 6th slash:
^((?:[^/]*/){6})
You may use
pages\/(?:[^\/]+\/){2}
And replace with pg/.
See the regex demo.
Details
pages\/ - a pages/ substring
(?:[^\/]+\/){2} - two repetitions of
[^\/]+ - 1 or more chars other than /
\/ - a / char.
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
I am trying to make a regular expression to find out the third delimiter "/" and if match found then grab entire sentence before it and delete rest.
black / white / green / blue / delta / orange / yellow / pink...(n)
outcome should be
black / white / green
I tried this (.*(\/{2,3})) but it did not work.
Your (.*(\/{2,3})) matches any chars (perhaps, other than a newline) and then 2 or 3 / symbols, capturing the whole match into Group 1, and the forward slashes into Group 2. It may find the match anywhere inside a larger string, but it just effectively matches from the start of the string up to the last 2 or 2 / symbols on a line.
You can use
^((?:[^\/]*\/){3}).*
And replace with the $1 backreference.
See the regex demo
Depending on the input and the regex engine, you might need a DOTALL modifier for the . to match newlines, too.
Note that you can use a simpler ^((?:[^\/]*\/){3}) for matching your substring.
Also, if you have access to raw programming code, you could just split the string with /, take the first 3 elements, and join them back with /.
Try this one:
^(( ?\w+ \/){3})
Explanation:
( ?\w+ /) - it's matching white/ (_ is space, just marking here), but since first word is at the beggining of the line, it does not contain a space before word itself, so '?' after first space, which means optional.
{3} means find this group white/ occuring 3 times (no less, no bigger)
And all this is enclosed with ^() to not match f.e. 3 occurrences in the middle of the line.