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!
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.
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.
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.
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(/.*\/(.*)$/);
Use this Regex pattern: /([^/]*)$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With