10

I have a string like fcsNotificationZK44_0376300009215000019_4158794.xml and a pattern (fcs.*)_ the target is to get fcsNotificationZK44 but standart C# Regex matched at fcsNotificationZK44_0376300009215000019 becouse it is greedy. In javascript I can use /U modifier to turn on ungreedy mode but how to solve it in C# Regex? Thanks.

Alexey Kulikov
  • 1,097
  • 1
  • 14
  • 38
  • Does this answer your question? [Regex Non-Greedy (Lazy)](https://stackoverflow.com/questions/13844168/regex-non-greedy-lazy) – Heretic Monkey Jan 06 '21 at 17:30

2 Answers2

16

Use *? to make the * non-greedy.

Reference: http://www.regular-expressions.info/repeat.html

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
0

You could use a not operator:

(fcs[^_]*)_
Jim Sosa
  • 578
  • 3
  • 21