正则表达式:匹配最后一次出现的“/”和“/”之间的模式和行尾

发布于 2024-11-01 06:20:55 字数 455 浏览 3 评论 0原文

我不知道如何匹配 LAST / 和行尾之间的模式。

我有很多:
/usr/etc/blabla:/etc/bbb
/usr/etc/blabla:/etc/bffb.gh
/usr/etc/blabla:/local/fffusr
/usr/etc/blabla:/bin/dfusrd
/usr/etc/var:/etc/aaaaaf.ju

例如,我只想匹配“usr”,仅当它位于粗体部分时。
我正在使用 grep。

编辑:
我对此解决方案有一个小问题:
/([^/]+)$
如果它紧接在 / 之后,则与模式不匹配,例如:
/usr/etc/blabla:/bin/usrlala
/bin/bla/:/etc/usr
不匹配

找到它:/([^/]*)$

I can't figure out how to match a pattern between LAST / and the end of the line.

I have tons of:
/usr/etc/blabla:/etc/bbb
/usr/etc/blabla:/etc/bffb.gh
/usr/etc/blabla:/local/fffusr
/usr/etc/blabla:/bin/dfusrd
/usr/etc/var:/etc/aaaaaf.ju

For example i want to match "usr" only when it is in the bold part.
I'm using grep.

EDIT:
I've a small problem with this solution:
/([^/]+)$
It doesn't match the pattern if it is immediately after the /, for example those:
/usr/etc/blabla:/bin/usrlala
/bin/bla/:/etc/usr
are not matched

FOUND IT: /([^/]*)$

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

泛泛之交 2024-11-08 06:20:56

它将是:

/([^/]+)$

但也许您必须根据您的语言转义斜杠(/):

/\/([^\/]+)$/

It would be:

/([^/]+)$

But maybe you must escape the slash (/) depending on your language:

/\/([^\/]+)$/
苏辞 2024-11-08 06:20:56

为什么要在如此简单的任务上使用正则表达式?

如果您使用的是 php,您可以使用

$pos = strrpos($line, '/'); 

来确定 / 最后一次出现,然后从那里复制所有内容

$name = substr($line, $pos+1);

正则表达式并不是所有问题的最终解决方案。在这种简单的字符串操作上它会变慢。好吧,你自己的程序解析字符串总是会慢一些(如果写得好的话)。

Why do you want to use regex on such simple task?

If you're using php you can use

$pos = strrpos($line, '/'); 

to determine last occurance of / and then copy everything from there

$name = substr($line, $pos+1);

regex is not ultimate solution to everything. It will be slower on such simple string operations. Well, it will always be slower to your own procedure parsing a string (if it's written good).

神妖 2024-11-08 06:20:56
echo "
/usr/etc/blabla:/etc/bbb
/usr/etc/blabla:/etc/bffb.gh
/usr/etc/blabla:/local/fffusr
/usr/etc/blabla:/bin/dfusrd
/usr/etc/var:/etc/aaaaaf.ju" | sed -n 's#.*/##;/.*usr.*/p'
fffusr
dfusrd
echo "
/usr/etc/blabla:/etc/bbb
/usr/etc/blabla:/etc/bffb.gh
/usr/etc/blabla:/local/fffusr
/usr/etc/blabla:/bin/dfusrd
/usr/etc/var:/etc/aaaaaf.ju" | sed -n 's#.*/##;/.*usr.*/p'
fffusr
dfusrd
寄人书 2024-11-08 06:20:56

用 JavaScript 回答

var s = "/usr/etc/var:/etc/aaaaaf.ju"
s ; //# => /usr/etc/var:/etc/aaaaaf.ju
var last = s.match(/[^/]+$/);
last ; //# => aaaaaf.ju

answer in javascript

var s = "/usr/etc/var:/etc/aaaaaf.ju"
s ; //# => /usr/etc/var:/etc/aaaaaf.ju
var last = s.match(/[^/]+$/);
last ; //# => aaaaaf.ju
潦草背影 2024-11-08 06:20:56

使用 PCRE:

$re = '/.+\/.*usr.*/i';

$string = '/usr/etc/blabla:/etc/bbb
/usr/etc/blabla:/etc/bffb.gh
/usr/etc/blabla:/local/fffusr
/usr/etc/blabla:/bin/dfusrd
/usr/etc/var:/etc/aaaaaf.ju';

$nMatches = preg_match_all($re, $string, $aMatches);

结果:

Array
(
    [0] => Array
        (
            [0] => /usr/etc/blabla:/local/fffusr
            [1] => /usr/etc/blabla:/bin/dfusrd
        )

)

Using PCRE:

$re = '/.+\/.*usr.*/i';

$string = '/usr/etc/blabla:/etc/bbb
/usr/etc/blabla:/etc/bffb.gh
/usr/etc/blabla:/local/fffusr
/usr/etc/blabla:/bin/dfusrd
/usr/etc/var:/etc/aaaaaf.ju';

$nMatches = preg_match_all($re, $string, $aMatches);

Result:

Array
(
    [0] => Array
        (
            [0] => /usr/etc/blabla:/local/fffusr
            [1] => /usr/etc/blabla:/bin/dfusrd
        )

)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文