Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expressions C# - is it possible to extract matches while matching?

Say, I have a string that I need to verify the correct format of; e.g. RR1234566-001 (2 letters, 7 digits, dash, 1 or more digits). I use something like:

        Regex regex = new Regex(patternString);         if (regex.IsMatch(stringToMatch))         {             return true;         }         else         {             return false;         } 

This works to tell me whether the stringToMatch follows the pattern defined by patternString. What I need though (and I end up extracting these later) are: 123456 and 001 -- i.e. portions of the stringToMatch.

Please note that this is NOT a question about how to construct regular expressions. What I am asking is: "Is there a way to match and extract values simultaneously without having to use a split function later?"

like image 519
sarsnake Avatar asked May 08 '09 21:05

sarsnake


People also ask

What is difference [] and () in regex?

This answer is not useful. Show activity on this post. [] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.

What is regex h in C?

DESCRIPTION. The <regex. h> header defines the structures and symbolic constants used by the regcomp(), regexec(), regerror() and regfree() functions.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).


1 Answers

You can use regex groups to accomplish that. For example, this regex:

(\d\d\d)-(\d\d\d\d\d\d\d) 

Let's match a telephone number with this regex:

var regex = new Regex(@"(\d\d\d)-(\d\d\d\d\d\d\d)"); var match = regex.Match("123-4567890"); if (match.Success)     .... 

If it matches, you will find the first three digits in:

match.Groups[1].Value 

And the second 7 digits in:

match.Groups[2].Value 

P.S. In C#, you can use a @"" style string to avoid escaping backslashes. For example, @"\hi\" equals "\\hi\\". Useful for regular expressions and paths.

P.S.2. The first group is stored in Group[1], not Group[0] as you would expect. That's because Group[0] contains the entire matched string.

like image 172
Andomar Avatar answered Oct 02 '22 12:10

Andomar