7

I have a question that come to my mind when I answered this post to match ASCII characters except alphanumeric.

This is what I have tried but it's not correct.

(?=[\x00-\x7F])[^a-zA-Z0-9]

regex101 demo

I am not looking for solution, just want to know, where I am wrong. What is the meaning of this regex pattern?

Thanks


As per my understanding (?=[\x00-\x7F]) is used to check for ASCII character and [^a-zA-Z0-9] is used to exclude alphanumeric character. So finally it will match any ASCII character except alphanumeric. Am I right?

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76

1 Answers1

1

The regex parser goes to each character in the string and checks it with the regex.

The first part, (?=...), is called a 'lookahead', and it asks if the next character is whatever specified (that is, [\x00-\x7F]). It doesn't move the character pointer.

The next part is saying that the next character is not alphanumeric, but does move the character pointer.

So it does precisely what you told it to; that is, match any non-alphanumeric ASCII character.

It does not match £ in ££££A$££0#$% because £ is not ASCII. If you want to match ANY character that is non-alphanumeric, you're probably looking for this regex:

`[^a-zA-Z0-9]`

See http://www.regular-expressions.info/lookaround.html and other pages on the site for more info.

oink
  • 1,443
  • 2
  • 14
  • 23
  • I know `£` is not an ASCII character but my question is about *match ASCII characters except alphanumeric*. So any ASCII character other than `[a-zA-Z0-9]` should be matched. `[^a-zA-Z0-9]` will match `non-ASCII` character as well that I don't want to match. – Braj Aug 18 '14 at 07:21
  • I don't want to match **ANY character** that is non-alphanumeric. – Braj Aug 18 '14 at 07:24
  • @user3218114 ...But then your original code works. ?_? – oink Aug 26 '14 at 00:42
  • I just answered the same answer [here](http://stackoverflow.com/questions/25349213/regex-to-match-ascii-non-alphanumeric-characters) but I got -2 votes that's why I asked it here to confirm it again. Thanks for your time. I don't why people down votes without any comments? – Braj Aug 26 '14 at 04:38