64

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

CodeCrack
  • 4,663
  • 11
  • 37
  • 68
  • 7
    An awesome resource for these kinds of questions is http://regexr.com (go here: http://regexr.com?2vpj8 for your actual expression and if you hover over each part of it, you get a nice description of what that rule does). – Chris Cashwell Jan 20 '12 at 17:43
  • 1
    An awesome resource for these kinds of questions is http://regexr.com ([go here](http://regexr.com?2vpj8) for your actual expression and if you hover over each part of it, you get a nice description of what that rule does). – Chris Cashwell Jan 20 '12 at 17:49

7 Answers7

50

In original question, just a backslash is needed before slash, in this case regex will get everything after last slash in the string

([^\/]+$)
Behzad
  • 703
  • 5
  • 11
37

No, an ^ inside [] means negation.

[/] stands for 'any character in set [/]'.

[^/] stands for 'any character not in set [/]'.

Dmitry Ovsyanko
  • 1,340
  • 9
  • 6
  • 1
    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. – CodeCrack Jan 20 '12 at 17:56
  • 6
    @CodeCrack `[^/]` matches one non-slash; `[^/]+` matches the first non-slashes only substring; `[^/]+$` matches the non-slashes substring right at the end of what you test. – Dmitry Ovsyanko Jan 23 '12 at 16:07
  • 1
    Please edit the answer to speak directly to the question instead of just having it in the comments. – Hack-R Jul 04 '17 at 03:43
4

Just fyi, for a non-regex version of this, depending on the language you're in, you could use a combination of the substring and lastIndexOf methods. Just find the last index of the "/" character, and get the substring just after it.

i.e., in Java

String targetString = "a/string/with/slashes/in/it";
int lastSlashIndex = targetString.lastIndexOf('/');
String everythingAfterTheFinalSlash = targetString.substring(lastSlashIndex + 1);
user1696017
  • 131
  • 11
2

Within brackets, ^/ means NOT A /. So this is matching a sequence of non-/'s up to the end of the line.

Scott Hunter
  • 44,196
  • 8
  • 51
  • 88
1

^ at the start of [] is character class negation. [...] specifies a set of characters to match. [^...] means match every character except that set of characters.

So [^/] means match every possible character except /.

ʞɔıu
  • 43,326
  • 30
  • 94
  • 142
0

if you put the ^ in a group it says all charters not in this group. So match all charter that are not slashes until the end of line $ anchor.

rerun
  • 23,827
  • 6
  • 44
  • 74
0

No, the ^ means different things depending on context. When inside a character class (the [] ), the ^ negates the expression, meaning "match anything except /.

Outside of [], the ^ means what you just said.

jmatias
  • 91
  • 2
  • 2
  • 7