Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 634
user815460 Avatar asked Jun 27 '11 15:06

user815460


People also ask

Which matches the start and end of the string?

Explanation: '^' (carat) matches the start of the string. '$' (dollar sign) matches the end of the string. Sanfoundry Certification Contest of the Month is Live. 100+ Subjects.

How do you end a regular expression in RegEx?

The correct regex to use is ^\d+$. Because “start of string” must be matched before the match of \d+, and “end of string” must be matched right after it, the entire string must consist of digits for ^\d+$ to be able to match.

How do you search for a RegEx pattern at the beginning of a string?

The meta character “^” matches the beginning of a particular string i.e. it matches the first character of the string. For example, The expression “^\d” matches the string/line starting with a digit. The expression “^[a-z]” matches the string/line starting with a lower case alphabet.


2 Answers

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(/.*\/(.*)$/); 
like image 78
mrk Avatar answered Sep 20 '22 19:09

mrk


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

like image 33
Kirill Polishchuk Avatar answered Sep 20 '22 19:09

Kirill Polishchuk