非贪婪的 LookAhead

发布于 2024-07-27 14:45:14 字数 148 浏览 1 评论 0原文

我有如下字符串:

val:key

我可以使用 /^\w*/ 捕获“val”。

现在我怎样才能获得没有“:”符号的“密钥”?

谢谢

I have strings like follows:

val:key

I can capture 'val' with /^\w*/.

How can I now get 'key' without the ':' sign?

Thanks

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

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

发布评论

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

评论(4

夜血缘 2024-08-03 14:45:14

这个怎么样?

/^(\w+):(\w+)$/

或者,如果您只想捕获冒号之后的所有内容:

/:(.+)/

这是一个不太清楚的示例,使用后向断言来确保冒号出现在匹配之前 - 整个匹配将不包括该冒号。

/(?<=:).*/

How about this?

/^(\w+):(\w+)$/

Or if you just want to capture everything after the colon:

/:(.+)/

Here's a less clear example using a lookbehind assertion to ensure a colon occurred before the match - the entire match will not include that colon.

/(?<=:).*/
诺曦 2024-08-03 14:45:14

您使用什么语言? /:(.*)/ 不会捕获“:”,但它匹配

Perl中的“:”,如果你说:

$text =~ /\:(.*)/;
$capture = $1;
$match = 
amp;;

那么$ capture不会有“:”和 $match 将。 (但尽量避免使用 $& ,因为它会减慢 Perl 的速度:这只是为了说明匹配)。

What language are you using? /:(.*)/ doesn't capture the ":" but it does match the ':'

In Perl, if you say:

$text =~ /\:(.*)/;
$capture = $1;
$match = 
amp;;

Then $capture won't have the ":" and $match will. (But try to avoid using $& as it slows down Perl: this was just to illustrate the match).

情独悲 2024-08-03 14:45:14

这将捕获组 1 中的键和组 2 中的值。即使值包含冒号 (:) 字符,它也应该正常工作。

^(\w+?):(.*)

This will capture the key in group 1 and the value in group 2. It should work correctly even when the value contails a colon (:) character.

^(\w+?):(.*)
梦断已成空 2024-08-03 14:45:14
/\:(\w*)/ 

查找 : 然后捕获它后面的所有单词字符,直到字符串末尾

/\:(\w*)/ 

That looks for : and then captures all the word characters after it till the end of the string

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