如何匹配但不捕获正则表达式的一部分? [英] How to match, but not capture, part of a regex?

查看:46
本文介绍了如何匹配但不捕获正则表达式的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串列表.其中一些是 123-...456 的形式.可变部分..."可能是:

I have a list of strings. Some of them are of the form 123-...456. The variable portion "..." may be:

  • 字符串apple"后跟一个连字符,例如123-apple-456
  • 字符串banana"后跟一个连字符,例如123-banana-456
  • 一个空字符串,例如123-456(注意只有一个连字符)
  • the string "apple" followed by a hyphen, e.g. 123-apple-456
  • the string "banana" followed by a hyphen, e.g. 123-banana-456
  • a blank string, e.g. 123-456 (note there's only one hyphen)

除apple"或banana"之外的任何词都是无效的.

Any word other than "apple" or "banana" is invalid.

对于这三种情况,我想分别匹配apple"、banana"和".请注意,我从不希望捕获连字符,但我总是希望匹配它.如果字符串不是如上所述的 123-...456 形式,则根本没有匹配项.

For these three cases, I would like to match "apple", "banana", and "", respectively. Note that I never want capture the hyphen, but I always want to match it. If the string is not of the form 123-...456 as described above, then there is no match at all.

如何编写正则表达式来执行此操作?假设我的风格允许前瞻、后视、环视和非捕获组.

How do I write a regular expression to do this? Assume I have a flavor that allows lookahead, lookbehind, lookaround, and non-capturing groups.

这里的关键观察是,当您有apple"或banana"时,您还必须有尾随连字符,但您不想匹配它.当您匹配空白字符串时,不能结尾连字符.我认为,封装此断言的正则表达式将是正确的.

The key observation here is that when you have either "apple" or "banana", you must also have the trailing hyphen, but you don't want to match it. And when you're matching the blank string, you must not have the trailing hyphen. A regex that encapsulates this assertion will be the right one, I think.

推荐答案

不捕获某些东西的唯一方法是使用 环视断言:

The only way not to capture something is using look-around assertions:

(?<=123-)((apple|banana)(?=-456)|(?=456))

因为即使使用 非捕获组(?:...) 整个正则表达式捕获它们匹配的内容.但是这个正则表达式只匹配 applebanana 如果它前面是 123- 并且后面是 -456,或者如果前面是 123- 后跟 456,它匹配空字符串.

Because even with non-capturing groups (?:…) the whole regular expression captures their matched contents. But this regular expression matches only apple or banana if it’s preceded by 123- and followed by -456, or it matches the empty string if it’s preceded by 123- and followed by 456.

<头>
环视姓名它的作用
(?=foo)前瞻断言紧跟字符串中当前位置的是foo
(?<=foo)回顾断言字符串中当前位置的前面是 foo
(?!foo)负前瞻断言紧跟字符串中当前位置的不是 foo
(?负面回顾断言字符串中当前位置之前的不是 foo

这篇关于如何匹配但不捕获正则表达式的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆