Alexey Kulikov
Alexey Kulikov

Reputation: 1147

Ungreedy regex in C#

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.

Upvotes: 12

Views: 4276

Answers (2)

Jim Sosa
Jim Sosa

Reputation: 628

You could use a not operator:

(fcs[^_]*)_

Upvotes: 0

NightOwl888
NightOwl888

Reputation: 56869

Use *? to make the * non-greedy.

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

Upvotes: 18

Related Questions