Alex Rivera | Logout

Regular expression "^[a-zA-Z]" or "[^a-zA-Z]"

Asked 2010-05-07T18:29:51.257
28

Is there a difference between ^[a-zA-Z] and [^a-zA-Z]?

When I check in C#,

Regex.IsMatch("t", "^[a-zA-Z]")  // Returns true (I think it's correct)

Regex.IsMatch("t", "[^a-zA-Z]")  // Returns false

There are a lot of web sites using [^a-zA-Z] for the alphabet. I'm not really sure which one is correct answer.

Edit
Report

1 Answer

74

Yes, the first means "match all strings that start with a letter", the second means "match all strings that contain a non-letter". The caret ("^") is used in two different ways, one to signal the start of the text, one to negate a character match inside square brackets.

answered 2010-05-07T18:31:52.050

Your Answer