Matches ASCII characters except alphanumeric

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

This is what I tried, but it is not correct.

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

demo version of regex101

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

thanks


According to my understanding (?=[\x00-\x7F]) used to check the ASCII character, and [^a-zA-Z0-9] used to exclude the alphanumeric character. Thus, it will match any ASCII character except alphanumeric. I'm right?

+7
java regex
source share
1 answer

The regular expression parser goes to each character in the string and checks it with a regular expression.

The first part, (?=...) , is called "lookahead", and asks if the next character has everything that is specified (ie [\x00-\x7F] ). It does not move the pointer to a character.

The next part says that the next character is not alphanumeric, but moves the pointer to the character.

And so it does what you told him; i.e. match any non-alphanumeric ASCII character.

It does not match £ in £ ££££A$££0#$% £ ££££A$££0#$% , because £ not ASCII. If you want to match ANY character that is not alphanumeric, you are probably looking for this regular expression:

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

For more details see http://www.regular-expressions.info/lookaround.html and other pages on the site.

+1
source share

All Articles