refering to already existing group in regex, c#

若如初见. 提交于 2019-12-08 08:39:09

问题


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#.


回答1:


using String.Format to avoid repetition and no there is no way to repeat the regex group literally

String.Format("{0}([<]{0})*", @"("".*""|[a-zA-Z]+)")



回答2:


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.



来源:https://stackoverflow.com/questions/638113/refering-to-already-existing-group-in-regex-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!