83

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
  • 5,003
  • 11
  • 40
  • 71
  • 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

69

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

([^\/]+$)
Behzad
  • 893
  • 6
  • 11
42

No, an ^ inside [] means negation.

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

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

Dmitry Ovsyanko
  • 1,400
  • 10
  • 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
  • 7
    @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
5

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
  • 140
  • 1
  • 12
2

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

Scott Hunter
  • 46,905
  • 10
  • 55
  • 92
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
  • 45,214
  • 31
  • 101
  • 144
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
  • 24,358
  • 6
  • 47
  • 75
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
  • 89
  • 2
  • 2
  • 7