Regex to match string that does not contain slash - regex

I am trying to set up a route using vue-router in a web app using regex to match the pattern. The pattern I am looking to match is any string that contains alphanumeric characters (and underscore) without slashes. Here are some examples (the first slash is just to show the string after the domain e.g. example.com/):
/codestack
/demo45
/i_am_long
Strings that should not match would be:
/data/files.xml
/share/home.html
/demo45/photos
The only regex I came up with so far is:
path: '/:Username([a-zA-Z0-9]+)'
That is not quite right because it matches all the characters except for the slash. Whereas I want to only match on the first set of alphanumeric characters (including underscore) before the first forward slash is encountered.
If a route contains a forward slash e.g. /data/files.xml then that should be a different regex route match. Therefore I also need a regex pattern to match the examples above containing slashes. Theoretically, they could contain any number of slashes e.g. /demo45/photos/holiday/2015/bahamas.

For the first part, you can match 1 or more word characters which will also match an underscore.
The anchors ^ and $ assert the start and end of the string.
^\w+$
For the second one, you can start the match with word characters followed by /
In case of more forward slashes you can optionally repeat the first pattern in a group.
The last part after the pattern can be 1 or more word characters with a optional part matching a dot and word characters.
^\w+/(?:\w+/)*\w+(?:\.\w+)?$
Regex demo
If you want to match any char except / you can use [^/]
^(?:[^/\s]+/)+[^/\s]+$
Regex demo

Related

Regular Expression for url paths

I want a regular expression to match on paths containing '/food/' but not '/food/api/':
http://example.com/food/api/pasta?sauce=true
Right now I'm using this:
/^((?!\/food\/api\/).)*$/
The problem with this is it matches ANY path that doesn't contain '/food/api/'
Behavior I want to achieve:
REGEX MATCHES
example.com/food/
example.com/food/meals
REGEX IGNORES
example.com/food/api/pasta?sauce=true
example.com/food/api/pasta
example.com/food/api/
example.com/meal
example.com/
Using a pattern like this ((?!\/food\/api\/).)* (a tempered greedy token solution) will match the whole line if it does not contain the sub string /food/api
As the quantifier is a * it will also match an empty line.
Instead, you can use an alternation to match until the first occurrence of a / followed by food or meal followed and a forward slash. After this slash, check that it is not followed by /api
^[^/]+/(?:food|meal)/(?!api/).*$
Regex demo
If the string can not contains spaces, you can exclude them using the negated character class [^/\s]+ and match \S* instead of .*
^[^/\s]+/(?:food|meal)/(?!api/)\S*$
Regex demo

Regex match last occurrence of substring among the same substrings in the string

For example we have a string:
asd/asd/asd/asd/1#s_
I need to match this part: /asd/1#s_ or asd/1#s_
How is it possible to do with plain regex?
I've tried negative lookahead like this
But it didn't work
\/(?:.(?!\/))?(asd)(\/(([\W\d\w]){1,})|)$
it matches this '/asd/asd/asd/asd/asd/asd/1#s_'
from this 'prefix/asd/asd/asd/asd/asd/asd/1#s_'
and I need to match '/asd/1#s_' without all preceding /asd/'s
Match should work with plain regex
Without any helper functions of any programming language
https://regexr.com/
I use this site to check if regex matches or not
here's the possible strings:
prefix/asd/asd/asd/1#s
prefix/asd/asd/asd/1s#
prefix/asd/asd/asd/s1#
prefix/asd/asd/asd/s#1
prefix/asd/asd/asd/#1s
prefix/asd/asd/asd/#s1
and asd part could be replaced with any word like
prefix/a1sd/a1sd/a1sd/1#s
prefix/a1sd/a1sd/a1sd/1s#
...
So I need to match last repeating part with everything to the right
And everything to the right could be character, not character, digit, in any order
A more complicated string example:
prefix/a1sd/a1sd/a1sd/1s#/ds/dsse/a1sd/22$$#!/123/321/asd
this should match that part:
/a1sd/22$$#!/123/321/asd
If you want the match only, you can use \K to reset the match buffer right before the parts that you want to match:
^.*\K/a\d?sd/\S+
The pattern will match
^ Start of string
.* Match any char except a newline until end of the line
\K Forget what is matched until now
/a\d?sd/ match a, optional digits and sd between forward slashes
\S+ Match 1+ non whitespace chars
See a regex demo

Match all characters after the last instance of a string in regex

I am looking to capture all characters after the last instance of a string in regex.
The string (that which we're searching after the last instance of) is as follows, sans quotes: " - ", or \b\s\-\s\b: boundary(whitespace character, preceded by -, preceded by whitespace character).
Test string as follows:
One Thing - Two Things - Three Things - Four Things
Desired match:
Four Things
This regex only matches everything after the first instance of the string:
(?<=\b\s\-\s\b)(.*)$
(Returns, sans quotes: "Two Things - Three Things - Four Things")
Whereas this matches everything after the last single character -:
[^\-]+$
(Returns, sans quotes: " Four Things")
Thoughts?
Try using a positive lookbehind then negating on the - delimiter and taking the last result
(?<=- )[^-]+$
https://regex101.com/r/sMX9FC/1
I think you could get your match without using lookarounds.
You could match any char except a newline from the start of the string followed by matching your pattern. That will match the last instance.
Then capture in a group matching 0+ times any char except a newline until the end of the string.
^.*\b\s\-\s\b(.*)$
^ Start of string
.* Match any char except a newline
\b\s\-\s\b\ Match your pattern
(.*) Capture in group 1 matching 0+ times any char except a newline
$ End of string
Regex demo
The is no tool or programming language listed, but if \K is supported to forget what was matched, you might also use:
^.*\b\s\-\s\b\K.*$
Regex demo
This matches the end of a string, everything that is not a - after a -.
-\s*([^-]+)$
It's the simplest regex I could think of.
.*(?<=\b\s\-\s\b)(.*)$, or putting a .* before your current regex should achieve what you're after, since that's a greedy match by default.

Regex to match characters to the right of a colon

I'm stuck on a regex. I'm trying to match words in any language to the right of a colon without matching the colon itself.
The basic rule:
For a line to be valid, it must not begin with or contain any characters outside of [a-z0-9_] until after :.
Any characters to the right of : should match as long as the line begins with the set of characters defined above.
For instance, given a string such as these:
this string should not match
bob_1:Hi. I'm Bob. I speak русский and this string should match
alice:Hi Bob. I speak 한국어 and this string should also match
http://example.com - would prefer to not match URLs
This string:should not match because no spaces or capital letters are allowed left of the colon
Only 2 of the 5 strings above need to match. And only to the right of the colon.
Hi. I'm Bob. I speak русский and this string should match
Hi Bob. I speak 한국어 and this string should also match
I'm currently using (^[a-z0-9_]+(?=:)) to match characters to the left of :. I just can't seem to reverse the logic.
The closest I have at the moment is (?!(?!:)).+. This seems to match everything to right of the colon as well as the colon itself. I just can't figure out how to not include : in the match.
Can one of you regex wizards help me out? If anything is unclear please let me know.
Short regex pattern (case insensitive):
^\w+:(\w.*)
\w - matches any word character (equal to [a-zA-Z0-9_])
https://regex101.com/r/MZhqSL/6
As you marked pcre, here's the pattern you need (only to the right of the colon):
^\w+:\K\w.*
\K - resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match
https://regex101.com/r/E1yHVY/1
You can use this regex:
^[a-z0-9_]+:\K(?!//).*
RegEx Demo
RegEx Breakup:
^: Start
[a-z0-9_]+: Match 1+ of [a-z0-9_] characters
:: Match a colon
\K: Reset matched info so far
(?!//): Negative lookahead to disallow // right after colon to avoid matching potential URLs
.*: Match anything until end
You can use the regex: ^.*?:(.*)$
^.*?: - from the beginning of the line, any character until the colon (non-greedy) included
(.*)$ - use a matching group to anything that follows it till the end of the line
Link to DEMO

Match a String with optional number of hyphens - Java Regex

I am trying to match Strings with optional number of hyphens.
For example,
string1-string2,
string1-string2-string3,
string1-string2-string3 and so on.
Right now, I have something which matches one hyphen. How can I make the regex to match optional number of hyphens?
My current regex is: arn:aws:iam::\d{12}:[a-zA-Z]/?[a-zA-Z]-?[a-zA-Z]*
What do I need to add?
Use this regex:
^\\w+(-\\w+)*$
Explanation:
\\w+ - match any string containing [a-zA-Z_0-9]
(-\\w+)* - match a hyphen followed by a string zero or more times
Regex101
Note that this won't match an empty string, or a string containing weird characters. You could handle these cases manually or you could update the regex.