-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
  • 101,031
  • 48
  • 228
  • 272
nam
  • 21,967
  • 37
  • 158
  • 332

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
  • 172,303
  • 28
  • 230
  • 274
3

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

Regex oRegex = new Regex(@"test[a-zA-Z]");
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
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
  • 101,031
  • 48
  • 228
  • 272
  • 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