Regex ignore middle part of capture

No, there is no facility to make a single match group containing non-contiguous text from the target string. You will need to use replace, or glue together the matching groups into a new string.


match a string that starts with 'first', has zero or more other characters, then ends with 'third'. Is that what you mean?

"^first(.*)third$"

Or, do you mean if you find a string 'firstsecondthird', ditch everything apart from 'first' and 'third'?

replace("^(first)second(third)$", "$1$2")

AFAIK, it is not possible to do with a single regular expression. You will have to use a call to replace(); as follows:

String inputVar = "firstsecondthird";
String resultVar = Regex.replace(inputVar, "^(first)second(third)$", "$1$2");

which can (typically...) be inserted into an expression as necessary

Tags:

C#

Regex