如何在不同的上下文中使用行首/行尾符号(^ 或 $)?

发布于 2024-08-27 14:01:44 字数 765 浏览 10 评论 0原文

在做一些小的正则表达式任务时,我遇到了这个问题。我有一个字符串,它是一个标签列表,例如如下所示:
foo,bar,qux,garp,wobble,thud

我需要做的是检查某个标签(例如“garp”)是否在此列表中。 (它最终匹配的内容并不重要,只要是否匹配即可。)

我的第一个也是有点愚蠢的尝试是使用以下正则表达式:
[^,]garp[,$]

我的想法是,在 'garp' 之前应该有行/字符串的开头或逗号,在 'garp' 之后应该有逗号或行/字符串的末尾。

现在,很明显这个正则表达式是错误的:^ 和 $ 在字符类 [ ] 的上下文中都改变了它们的行为。

我最终想出的是以下内容:
^garp$|^garp,|,garp,|,garp$

这个正则表达式只是一一处理这 4 种情况。 (标记在列表的开头、中间、末尾,或者作为列表的唯一元素。)最后一个正则表达式在我看来有点丑陋,只是为了好玩,我想让它变得有点丑陋更优雅。

有没有办法在字符类的上下文中使用行开头/行结尾字符(^ 和 $)?

编辑: 好的,希望有更多信息,所以这里是: 我在 Oracle SQL 语句中使用它。遗憾的是,这不允许任何环视断言,但因为我只感兴趣是否存在匹配(而不是匹配的内容),这并没有真正影响我。 标签可以包含非字母字符,例如 - 或 _,因此 \bgarp\b 不起作用。另外,正如 SilentGhost 所说,一个标签可以包含另一个标签,因此 /garp/ 也不起作用。

While doing some small regex task I came upon this problem. I have a string that is a list of tags that looks e.g like this:
foo,bar,qux,garp,wobble,thud

What I needed to do was to check if a certain tag, e.g. 'garp' was in this list. (What it finally matches is not really important, just if there is a match or not.)

My first and a bit stupid try at this was to use the following regex:
[^,]garp[,$]

My idea was that before 'garp' there should either be the start of the line/string or a comma, after 'garp' there should be either a comma or the end of the line/string.

Now, it is instantly obvious that this regex is wrong: Both ^ and $ change their behaviour in the context of the character class [ ].

What I finally came up with is the following:
^garp$|^garp,|,garp,|,garp$

This regex just handles the 4 cases one by one. (Tag at beginning of list, in the center, at the end, or as the only element of the list.) The last regex is somehow a bit ugly in my eyes and just for funs sake I'd like to make it a bit more elegant.

Is there a way how the start of line/end of line characters (^ and $) can be used in the context of character classes?

EDIT:
Ok, some more info was wished so here it is:
I'm using this within an Oracle SQL statement. This sadly does not allow any look-around assertions but as I'm only interested if there is a match or not (and not what is matched) this does not really affect me here.
The tags can contain non-alphabetical characters like - or _ so \bgarp\b would not work. Also one tag can contain an other tag as SilentGhost said, so /garp/ doesnt work either.

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

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

发布评论

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

评论(4

︶葆Ⅱㄣ 2024-09-03 14:01:45

没有正则表达式:

myString.Split(',').Contains("garp")

Without regex:

myString.Split(',').Contains("garp")
奶茶白久 2024-09-03 14:01:44

您不能按照您希望的方式在字符类中使用 ^$ - 它们将按字面解释,但您可以使用交替来实现相同的效果:

(^|,)garp(,|$)

You can't use ^ and $ in character classes in the way you wish - they will be interpreted literally, but you can use an alternation to achieve the same effect:

(^|,)garp(,|$)
流云如水 2024-09-03 14:01:44

您只需要使用字边界 (\b) 而不是 ^$

\bgarp\b

you just need to use word boundary (\b) instead of ^ and $:

\bgarp\b
逆夏时光 2024-09-03 14:01:44

只需使用环视来解决此问题:

(?<=^|,)garp(?=$|,)

环视和常规组的区别在于,对于常规组,逗号将成为匹配的一部分,而对于环视则不会。但在这种情况下,这没有什么区别。

Just use look-arounds to solve this:

(?<=^|,)garp(?=$|,)

The difference with look-arounds and just regular groups are that with regular groups the comma would be part of the match, and with look-arounds it wouldn't. In this case it doesn't make a difference though.

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