简体   繁体   English

正则表达式模式以匹配字符串的结尾

[英]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"; 例如,str =“ red / white / blue”;

I'd like to match "blue" because it is everything to the right of the last "/". 我想匹配“蓝色”,因为它位于最后一个“ /”右侧的所有位置

Many thanks! 非常感谢!

使用此Regex模式: /([^/]*)$

Use the $ metacharacter to match the end of a string. 使用$元字符来匹配字符串的结尾。

In Perl, this looks like: 在Perl中,这看起来像:

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

Written in JavaScript, this looks like: 用JavaScript编写,如下所示:

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

Should be 应该

~/([^/]*)$~

Means: Match a / and then everything, that is not a / ( [^/]* ) until the end ( $ , "end"-anchor). 意思是:先匹配一个/ ,然后匹配所有内容,直到最后一个( $ ,“ end” -anchor)才是/[^/]* )。

I use the ~ as delimiter, because now I don't need to escape the forward-slash / . 我将~用作定界符,因为现在不需要转义正斜杠/

Something like this should work: /([^/]*)$ 这样的事情应该起作用: /([^/]*)$

What language are you using? 您使用什么语言? End-of-string regex signifiers can vary in different languages. 字符串结尾的正则表达式指示符可以在不同的语言中变化。

使用以下模式:

/([^/]+)$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM