I was browsing stackoverflow and have noticed a regular expression for matching everything after last slash is
([^/]+$)
So for example if you have http://www.blah.com/blah/test The reg expression will extract 'test' without single quotes.
My question is why does it do it? Doesn't ^/ mean beginning of a slash?
EDIT: I guess I do not understand how +$ grabs "test". + repeats the previous item once or more so it ignores all data between all the / slashes. how does then $ extract the test
Literal Characters and Sequences For instance, you might need to search for a dollar sign ("$") as part of a price list, or in a computer program as part of a variable name. Since the dollar sign is a metacharacter which means "end of line" in regex, you must escape it with a backslash to use it literally.
means, "match the 'period' as a literal character". If you don't have the backslash "escape character", the in most Regexp engines means, "match any character". Follow this answer to receive notifications.
Throw in an * (asterisk), and it will match everything. Read more. \s (whitespace metacharacter) will match any whitespace character (space; tab; line break; ...), and \S (opposite of \s ) will match anything that is not a whitespace character.
The forward slash character is used to denote the boundaries of the regular expression: ? The backslash character ( \ ) is the escaping character. It can be used to denote an escaped character, a string, literal, or one of the set of supported special characters.
In original question, just a backslash
is needed before slash
, in this case regex will get everything after last slash
in the string
([^\/]+$)
No, an ^
inside []
means negation.
[/]
stands for 'any character in set [/]'.
[^/]
stands for 'any character not in set [/]'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With