92

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,253
  • 11
  • 44
  • 72
  • 8
    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

7 Answers7

80

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

([^\/]+$)
Behzad
  • 1,003
  • 7
  • 12
44

No, an ^ inside [] means negation.

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

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

Dmitry Ovsyanko
  • 1,416
  • 11
  • 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
  • 8
    @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
  • 2
    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
8

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
  • 170
  • 1
  • 13
3

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

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
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
  • 47,148
  • 35
  • 106
  • 149
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
  • 25,014
  • 6
  • 48
  • 78
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
  • I have a simillar problem I have `/db/env/dbo/analytical/data_from_google/sys_year=2022/sys_month=05/sys_day=14/part-00073-a4022c1a-c8a2-4af9-8289-0c3e514396e8.c002` how to use regexp to receive `/db/env/dbo/analytical/data_from_google/sys_year=2022/sys_month=05/sys_day=14/` i mean remove everything after last "/" – Mariusz Kowalewski Jun 22 '22 at 08:11