我想知道如果使用perl正则表达式,这个子字符串的出现次数可以是0或更多次,是否可以从字符串中剪切子字符串?
例如:
"foo bar //baz"并且如果它存在的话,它们都"foo bar"应该导致"foo bar"削减双斜线后面的所有东西.
我知道这可以通过其他方法轻松实现,但我很感兴趣,如果正则表达式oneliner是可能的.
我试过($new_string) = ($string =~ /(.*?)(\/\/)*.*/)
但是那不起作用.
_____________ Matches 0 chars at position 0 ("").
/ ______ Matches 0 chars at position 0 ("").
/ / _____ Matches 13 chars at position 0 ("foo bar //baz").
_/ _____/ /
/ \ / \/\
(.*?)(\/\/)*.*
Run Code Online (Sandbox Code Playgroud)
你想要什么:
( my $new_string = $string ) =~ s{//.*}{};
my $new_string = $string =~ s{//.*}{}r; # 5.14+
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
71 次 |
| 最近记录: |