引用正则表达式中已经存在的组,c#
我有一个正则表达式,其中
%word% can occur multiple times, separated by a "<"
%word% 定义为 ".*?"|[a-zA-Z]+
所以我写了
(".*"|[a-zA-Z]+)([<](".*"|[a-zA-Z]+))*
有什么方法可以使用捕获组缩小它吗?
(".*"|[a-zA-Z]+)([<]\1)*,
但我不认为 \1
可以使用,因为它意味着重复第一次捕获,因为我不知道捕获了什么,因为它可以是带引号的字符串或单词。
任何类似的东西我都可以用来引用匹配之前写的组。 我正在使用 C# 工作。
I have a regex where
%word% can occur multiple times, separated by a "<"
%word% is defined as ".*?"|[a-zA-Z]+
so i wrote
(".*"|[a-zA-Z]+)([<](".*"|[a-zA-Z]+))*
Is there any way i can shrink it using capturing groups?
(".*"|[a-zA-Z]+)([<]\1)*,
But i don't think \1
can be used as it'd mean repeat the first capture, as i would not know what was captured as it can be a quoted string or a word.
Any thing similar i can use to refer matching the previously written group. I'm working in C#.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 String.Format 来避免重复,并且没有办法从字面上重复正则表达式组
using String.Format to avoid repetition and no there is no way to repeat the regex group literally
由于尚不支持该功能,我制作了一个字符串替换器,其中我使用 %% 编写了需要用正则表达式替换的特定单词,然后编写程序以用为文本定义的正则表达式替换它。
As the support is not there yet for the feature, i made a string replacer, where i wrote the specific words i need to replaced by regex using %% and then wrote the program to replace it by the regular expression defined for the text.