用于跳过捕获组中字符的正则表达式 [英] Regular expression to skip character in capture group

查看:35
本文介绍了用于跳过捕获组中字符的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在正则表达式中跳过捕获组中的几个字符?我正在使用 .NET 正则表达式,但这无关紧要.

Is it possible to skip a couple of characters in a capture group in regular expressions? I am using .NET regexes but that shouldn't matter.

基本上,我正在寻找的是:

Basically, what I am looking for is:

[随机文字]AB-123[随机文字]

[random text]AB-123[random text]

我需要捕获AB123",不带连字符.

and I need to capture 'AB123', without the hyphen.

我知道 AB 是 2 或 3 个大写字符,而 123 是 2 或 3 位数字,但这不是难点.困难的部分(至少对我而言)是跳过连字符.

I know that AB is 2 or 3 uppercase characters and 123 is 2 or 3 digits, but that's not the hard part. The hard part (at least for me) is skipping the hyphen.

我想我可以分别捕获两者,然后在代码中将它们连接起来,但我希望我有一个更优雅的、仅使用正则表达式的解决方案.

I guess I could capture both separately and then concatenate them in code, but I wish I had a more elegant, regex-only solution.

有什么建议吗?

推荐答案

简而言之:你不能.匹配始终是连续的,即使它包含零宽度断言等内容,如果您想找到下一个字符,则无法匹配下一个字符.

In short: You can't. A match is always consecutive, even when it contains things as zero-width assertions there is no way around matching the next character if you want to get to the one after it.

这篇关于用于跳过捕获组中字符的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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