Regex Pattern that has to include something after / - regex

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.

Related

How do I make this regular expression not match anything after forward slash /

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.

Regex to properly match urls with a particular domain and also if there is a subdomain added

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

Get the last two path depths. Regex

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.

regex check if url has trailing slash and params

i need a check on regex, in particular:
/it/categoria/diritti-e-ugualianza
/it/categoria/diritti-e-ugualianza/
/it/categoria/diritti-e-ugualianza?i=1
/it/categoria/diritti-e-ugualianza/?i=1
must be checked in a unique rule
i try with this
/it/categoria/diritti-e-ugualianza(?:/(.*))?$
but it works only with
/it/categoria/diritti-e-ugualianza
/it/categoria/diritti-e-ugualianza/
exists a way to ignore also params?
thank you
You may replace / with a character class [/?] that matches either ? or /:
/it/categoria/diritti-e-ugualianza(?:[?/](.*))?$
^^^^
See the regex demo.
Details
/it/categoria/diritti-e-ugualianza - a literal substring
(?:[?/](.*))? - an optional group matching 1 or 0 occurrences of
[?/] - a ? or /
(.*) - Capturing group 1: any 0+ chars to the end of the line
$ - end of string.

Regex match if certain string is contained after last occurrence of specific character

For example, I want to check if the web url contains 'foo' after last slash, and match the entire url. So the following url should be a match:
https://www.facebook.com/messages/new/foobar
https://www.facebook.com/messages/t/barfoo
https://www.facebook.com/bfooar
https://foobar.com
https://foobar.com/foo
But the following shouldn't:
https://random.com/random
https://foobar.com/something
https://foobar.com/foo/bar
My approach is ((\\.*)*\\.*foo.*), but it seems doesn't work for any url that contains foo before the last slash. Is this pattern even doable in regex? Or I have to use something like split('\') in the code to achieve the desired pattern I want?
Thanks
You can use this regex:
^.*/[^/]*foo[^/]*$
RegEx Demo
Breakup:
^ - Start
.* - Match 0 or more characters (greedy)
/ - Match a /
[^/]* - Match 0 or more non-/ characters
foo - match foo
[^/]* - Match 0 or more non-/ characters
$ - End