Link to home
Start Free TrialLog in
Avatar of jadunham1
jadunham1

asked on

Search a Multiple Entry RegEx

Basically I have done this in Java and am just wondering how to do this in C#.
I want to search through a String made out of expressions to see if my entry matches any of them.

In Java I did a string with the regex's then made a Pattern to compile the multiple regex's in the string and then a Matcher to match them.

So for example in java

final String REGEX_LIST = "2\\d{4}|
543\\d{2}|
34432\\d4"

Then you can use Pattern.compile (REGEX_LIST)
and used Matcher to match my input against the REGEX_LIST.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jadunham1
jadunham1

ASKER

Thanks that answers my question.
Is there any reason you can't do
System.Text.RegularExpressions.RegEx reg = new System.Text.RegularExpressions.RegEx("2\\d{4}|543\\d{2}|34432\\d4");

because that is what I was trying to do.  You have to initialize the list as a string and then put that in the regular expression?
Hmm nevermind that seems to be working as well, not sure what I was doing wrong before.

Thanks!
No. What you just posted should work fine. You have doubled-up the backslashes for the shorthand operators (alternatively, you could use single-slashes and prefix the string with "@"-- e.g. @"2\d{4}|543\d{2}|34432\d4").

Below is a code sample that I tested (Note:  I mistyped Regex as RegEx...  C# is case-sensitive :) ):
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex("2\\d{4}|543\\d{2}|34432\\d4");
string sourceData = "54300abcde";

Console.WriteLine(reg.IsMatch(sourceData));  // Returns boolean

Console.ReadKey();

Open in new window