正则表达式:不要在组中包含子字符串

发布于 2024-10-28 22:15:38 字数 500 浏览 1 评论 0原文

我想知道是否可以在组中排除比赛的一部分。

我相信/(foo((?:bar)|(bad)))/导致匹配“ foobar”或“ foobad”和包含“ foobar”或“ foobad”的第1组。我希望它匹配“ Foobar”或“ Foobad”,但第1组仅包含“ Foo”或“ Foobad”。我知道 (?:...) 正则表达式语法用于创建一个稍后无法引用的组,但上述用法意味着有 3 个组: \1 整个匹配; \ 2“ bar”或“坏”; \没有一个未使用的团体“ bar”; \ 3为“坏”。

你看到我要去哪里?我想选择地从另一组中排除一个子组。

或者,我想以 OR 方式定义组 1 两次 /(foobar)|(foobad)/ 尽管我知道输入不会执行我想要的操作。在这种情况下,尝试访问匹配[1]如果给出的“ foobad”作为输入,则会导致“未定义”错误。我知道这是因为它实际上将其分为第1组和 /或组2

I'm wondering if it is possible to exclude part of a match within a group.

I believe that /(foo((?:bar)|(bad)))/ results in matching "foobar" or "foobad" and group 1 containing "foobar" or "foobad". I would like it to match "foobar" or "foobad" but group 1 contain only "foo" or "foobad". I know that the (?:...) regex syntax is used to make a group that you cannot reference later on, but the above usage means that there are 3 groups: \1 the entire match; \2 either 'bar' or 'bad'; \NONE being an unused group 'bar'; and \3 for 'bad'.

You see where I'm going here? I want to optionally exclude a sub group from within another group.

Alternatively, I would like to define group 1 twice in an OR fashion /(foobar)|(foobad)/ though I know that input wouldn't do what I wanted. In that case, trying to access match[1] results in an "undefined" error if given "foobad" as input. I know this is because it is actually splitting it into group 1 and or group 2.

I'm writing this in the javascript regex dialect.

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

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

发布评论

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

评论(3

柳絮泡泡 2024-11-04 22:15:38

我希望它匹配“foobar”或“foobad”,但组 1 仅包含“foo”或“foobad”。

正则表达式:

/(foo(?:(?=bar)|bad))/

的作用是:组 1 要么包含 foo(仅当 bar 紧随其后),要么包含 foobad

I would like it to match "foobar" or "foobad" but group 1 contain only "foo" or "foobad".

The regex:

/(foo(?:(?=bar)|bad))/

does that: group 1 either contains foo (only if bar comes directly after it) or it contains foobad.

夕嗳→ 2024-11-04 22:15:38

(?:bar) - 匹配 bar 但不记得匹配。这些称为非捕获括号。无法从结果数组的元素 [1], ..., [n] 或预定义的 RegExp 对象的属性 $1, ..., $9 中调用匹配的子字符串>。

更多信息:Mozilla 开发人员中心

(?:bar) - Matches bar but does not remember the match. These are called non-capturing parentheses. The matched substring can not be recalled from the resulting array's elements [1], ..., [n] or from the predefined RegExp object's properties $1, ..., $9.

Further info: Mozilla Developer Center.

她比我温柔 2024-11-04 22:15:38

这很接近您想要的,

/((?=foobar)foo|foobad)/

它是一个匹配 foo (但前提是从它开始匹配 foobar)或 foobad 的单个组>。 (?=...) 形式是零宽度先行断言。

这并不完全是你想要的,因为 foobar 的情况下的匹配只是 foo 但 IMO 能够让它匹配 foobar 但只将 foo 分组在同一组中,其中foob​​ad 会被匹配,而不需要lookbehind,并且据我所知,它在javascript中不可用。

This is close to what you want

/((?=foobar)foo|foobad)/

it's a single group that matches either foo (but only if starting with that it would have matched foobar) or foobad. The form (?=...) is a zero-width look-ahead assertion.

It's not exactly what you want because the match in case of foobar is just foo but IMO to be able to have it to match foobar but grouping only foo in the same group where foobad would be matched instead requires lookbehind and AFAIK it's not available in javascript.

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