i just want to have the last two path depths matched.
Example input:
/root/sync/test1/hallo12
Matched should be:
/test1/hallo12
My not working regex:
(/.*){2,2}$
You may use
(?:/[^/]+){2}$
See the regex demo.
Details
(?:/[^/]+){2} - two consecutive occurrences of
/ - a slash
[^/]+ - 1+ chars other than /
$ - end of string.
In case there can be a trailing slash use
(?:/[^/]+){2}/?$
^^
where /? matches 1 or 0 slashes.
Note that in case you are using the pattern inside / regex delimiters you have to escape the slashes in the pattern.
Related
Using Regex, I want to match any URL that includes the /it-jobs/ but must have something after the final /.
To be a match the URL must have /it-jobs/ + characters after the trailing / otherwise it should not match. Please refer to below example.
Example: www.website.com/it-jobs/ - is not a match
www.website.com/it-jobs/java-developer - is a match
www.website.com/it-jobs/php - is a match
www.website.com/it-jobs/angular-developer - is a match
You can use
/it-jobs/[^/\s]+$
To match the whole string, add .* at the pattern start:
.*/it-jobs/[^/\s]+$
See the regex demo.
Details:
.* - zero or more chars other than line break chars as many as possible
/it-jobs/ - a literal string
[^/\s]+ - any one or more chars other than / and whitespaces
$ - end of string.
I have this regular expression:
/^www\.example\.(com|co(\.(in|uk))?|net|us|me)\/?(.*)?[^\/]$/g
It matches:
www.example.com/example1/something
But doesn't match
www.example.com/example1/something/
But the problem is that, it matches: I do not want it to match:
www.example.com/example1/something/otherstuff
I just want it to stop when a slash is enountered after "something". If there is no slash after "something", it should continue matching any character, except line breaks.
I am a new learner for regex. So, I get confused easily with those characters
You may use this regex:
^www\.example\.(?:com|co(?:\.(?:in|uk))?|net|us|me)(?:\/[^\/]+){2}$
RegEx Demo
This will match following URL:
www.example.co.uk/example1/something
You can use
^www\.example\.(?:com|co(?:\.(?:in|uk))?|net|us|me)\/([^\/]+)\/([^\/]+)$
See the regex demo
The (.*)? part in your pattern matches any zero or more chars, so it won't stop even after encountering two slashes. The \/([^\/]+)\/([^\/]+) part in the new pattern will match two parts after slash, and capture each part into a separate group (in case you need to access those values).
Details:
^ - start of string
www\.example\. - www.example. string
(?:com|co(?:\.(?:in|uk))?|net|us|me) - com, co.in, co.uk, co, net, us, me strings
\/ - a / char
([^\/]+) - Group 1: one or more chars other than /
\/ - a / char
([^\/]+) - Group 2: one or more chars other than /
$ - end of string.
I have the following regex:
(^|^[^:]+:\/\/|[^\.]+\.)hello\.net
Which seems to work fors most cases such as these:
http://hello.net
https://hello.net
http://www.hello.net
https://www.hello.net
http://domain.hello.net
https://solutions.hello.net
hello.net
www.hello.net
However it still matches this which it should not:
hello.net.domain.com
You can see it here:
https://regex101.com/r/fBH112/1
I am basically trying to check if a url is part of hello.net. so hello.net and any subdomains such as sub.hello.net should all match.
it should also match hello.net/bye. So anything after hello.net is irrelevant.
You may fix your pattern by adding (?:\/.*)?$ at the end:
(^|^[^:]+:\/\/|[^.]+\.)hello\.net(?:\/.*)?$
See the regex demo. The (?:\/.*)?$ matches an optional sequence of / and any 0 or more chars and then the end of string.
You might consider a "cleaner" pattern like
^(?:\w+:\/\/)?(?:[^\/.]+\.)?hello\.net(?:\/.*)?$
See the regex demo. Details:
^ - start of string
(?:\w+:\/\/)? - an optional occurrence of 1+ word chars, and then :// char sqequence
(?:[^\/.]+\.)? - an optional occurrence of any 1 or more chars other than / and . and then .
hello\.net - hello.net
(?:\/.*)?$ - an optional occurrence of / and then any 0+ chars and then end of string
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.
I have a number of floats/strings that look as follows:
12339.0
133339
159.0
dfkkei
something
32439
Some of them have trailing .0. How can I show all the numbers without the trailing .0 as a regular repression, including the items that are not a number? I tried something like that, hoping it would exclude all .0 from the capture group, but it doesn't work: (.*)(:?.0)?
https://regex101.com/r/sC6jO2/1
You may use a simpler regex:
\.0+$
And replace with an empty string, see regex demo.
The regex matches a . (\.) followed with 1 or more zeros (0+) up to the end of string ($).
If you plan to match two groups as in your initial attempt, use
^(.*?)(?:\.0+)?$
See this regex demo
Here,
^ - start of string
(.*?) - Group 1 capturing any 0+ chars other than a newline, as few as possible (=lazily), up to a
(?:\.0+)? - optional sequence of . + one or more zeros
$ - at the end of the string.