-3
Regex oRegex = new Regex(@"test[a-zA-z]");
string st = @"this is a test1 and testA and test[abc] another testB and test(xyz) again.";
foreach(Match match in oRegex.Matches(st))
{
     Console.WriteLine(match.Value);
}

Output:

testA

test[

testB

Question: Why test[ in the output? The character class [a-zA-Z] is supposed to match only alpha characters a through z and A through Z.

Blorgbeard
  • 97,316
  • 47
  • 222
  • 267
nam
  • 18,769
  • 32
  • 131
  • 275

3 Answers3

3

Because [ falls within the ascii range A-z, so change A-z present inside the char class to A-Z

Regex oRegex = new Regex(@"test[a-zA-Z]");
Avinash Raj
  • 166,785
  • 24
  • 204
  • 249
3

Z is typo in your case .Change this [a-zA-Z]

Regex oRegex = new Regex(@"test[a-zA-Z]");
Nagaraj S
  • 12,994
  • 6
  • 31
  • 51
2

You have a typo in your regex. [a-zA-z] should be [a-zA-Z].

The character [ is between the A and z characters.

Blorgbeard
  • 97,316
  • 47
  • 222
  • 267
  • Thank you ALL for pointing out the typo. In a way the typo helped me understand the range A-z. This [SO post](http://stackoverflow.com/a/4923475/1232087) helped me explain further why "[" is included in the range A-z. – nam Jul 15 '15 at 05:11
  • @Blorgbeard I'm the first to post this answer how come your post displayed at first when clicking the oldest tab? – Avinash Raj Jul 15 '15 at 05:14
  • @AvinashRaj not sure. Mine is accepted now, so it will always display first. You can hover over the "2 hours ago" to see the timestamp - looks like you beat me by 10 seconds :) – Blorgbeard Jul 15 '15 at 07:09