user815460
user815460

Reputation: 1143

regex pattern to match the end of a string

Can someone tell me the regex pattern to match everything to the right of the last "/" in a string.

For example, str="red/white/blue";

I'd like to match "blue" because it is everything to the right of the last "/".

Many thanks!

Upvotes: 62

Views: 161889

Answers (5)

mrk
mrk

Reputation: 5127

Use the $ metacharacter to match the end of a string.

In Perl, this looks like:

my $str = 'red/white/blue';
my($last_match) = $str =~ m/.*\/(.*)$/;

Written in JavaScript, this looks like:

var str = 'red/white/blue'.match(/.*\/(.*)$/);

Upvotes: 50

Phillip Kovalev
Phillip Kovalev

Reputation: 2497

Use following pattern:

/([^/]+)$

Upvotes: 4

NickAldwin
NickAldwin

Reputation: 11754

Something like this should work: /([^/]*)$

What language are you using? End-of-string regex signifiers can vary in different languages.

Upvotes: 4

KingCrunch
KingCrunch

Reputation: 132051

Should be

~/([^/]*)$~

Means: Match a / and then everything, that is not a / ([^/]*) until the end ($, "end"-anchor).

I use the ~ as delimiter, because now I don't need to escape the forward-slash /.

Upvotes: 12

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56202

Use this Regex pattern: /([^/]*)$

Upvotes: 36

Related Questions