Imagine a very large regular schedule that takes everything between tags in one line, for example:

'>значение<' 

To do this, we write something like:

 >([^<]+) 

There are a lot of similar identical pieces of expression.

Can I put them in a group like:

 (?'value'[^<]+) 

and use in another piece of the same expression of the type >(value) ?

  • That is, to find a match in one place RV, and then check in another place of the same RV for equality? - Vesper
  • @Vesper, that is, enclose the subexpression in a named group and use its name elsewhere in the same expression, so as not to produce a bunch of identical letters. - anweledig
  • one
    It is easier to use string.Format() to "add" a regular expression. No in .NET \g<n> and recursion, as in PCRE. - Wiktor Stribiżew
  • @stribizhev, understood you, thank you - anweledig
  • Those. let's say there is a regexp for "email-address", it’s healthy, and you need to have it built up using regexp in two different places, a la "blabla (" email-address part ") foo (" email-address part ") boo "? It is easier to collect regex as a string in this scenario. Those. you have a string with regexp String email_address , and you join it to the necessary regexp, at least how many times you add it. - Vesper

1 answer 1

Since in .NET there is no \g<n> and no recursion, as in PCRE, it is not possible to use the reference to the part of the regular expression inside the regular expression.

For this, it is easier to use string.Format() :

 var something = "[^<]*"; var regex = new Regex(string.Format(">{0}<.*?>{0}<", something)); 

enter image description here

  • one
    string.Format(">{0}<.*?>{1}<", something, something) - what about ??? This is not a printf. string.Format(">{0}<.*?>{0}<", something) necessary! - Qwertiy
  • Sorry, messed up at work :) - Wiktor Stribiżew