1

My input is: abc1 abc2 abc3 abc4

I wont to get a, and any chars but as few times as possible, and 3.

the output must begins with a, and continues with every character in the world except for something corresponding to a, and ending with 3, where a is a Regax expression.

but if I use pattern: a.*?3, the match is: abc1 abc2 abc3, But I expecting to abc3.

I can't use a[^a]*?3 because a is unknown in advance.

How can I achieve this in .NET Regex?

google dev
  • 844
  • 10
  • 27

1 Answers1

1

You want to match some generic pattern, then match any text that does not match the value pattern up to the leftmost occurrence of the right-hand delimiter pattern.

In this case you need do

  • Capture the starting pattern into a group
  • Use a tempered greedy token with the backreference to the group value as the negative lookahead pattern, make sure the quantifier is non-greedy

Here is an example with a starting delimiter pattern that is obfuscated as (?![b-z])[a-z]:

((?![b-z])[a-z])(?:(?!\1).)*?3

See the regex demo. So, it matches

  • ((?![b-z])[a-z]) - Capturing group 1: any lowercase ASCII but a letter from b to z range (basically, a)
  • (?:(?!\1).)*? - any char, but a newline char, 0 or more occurrences (but as few as possible), that does not start the value stored in Group 1
  • 3 - the right-hand delimiter, 3.
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397