Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 654
Alexey Kulikov Avatar asked Mar 17 '15 17:03

Alexey Kulikov


People also ask

Can you do regex in C?

A regular expression is a sequence of characters used to match a pattern to a string. The expression can be used for searching text and validating input. Remember, a regular expression is not the property of a particular language. POSIX is a well-known library used for regular expressions in C.

What is \r and \n in regex?

Regex recognizes common escape sequences such as \n for newline, \t for tab, \r for carriage-return, \nnn for a up to 3-digit octal number, \xhh for a two-digit hex code, \uhhhh for a 4-digit Unicode, \uhhhhhhhh for a 8-digit Unicode.

What does \\ mean in regex?

\\. matches the literal character . . the first backslash is interpreted as an escape character by the Emacs string reader, which combined with the second backslash, inserts a literal backslash character into the string being read. the regular expression engine receives the string \. html?\ ' .

What is a greedy match regex?

Greedy: As Many As Possible (longest match) By default, a quantifier tells the engine to match as many instances of its quantified token or subpattern as possible. This behavior is called greedy. For instance, take the + quantifier.


1 Answers

Use *? to make the * non-greedy.

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

like image 179
NightOwl888 Avatar answered Nov 04 '22 02:11

NightOwl888