Jump to content

Help With Invalid Characters


adam84

Recommended Posts

Hi,

 

I wrote a regex that will look for alphanumeric values only.

/^\w+$/

 

What is happening is I am reading some data from a file and sometimes there is data that is invalid and it shows up as a square box. I am trying to develop a regex statement that will look for a character that is not [a-zA-Z0-9].

 

Any ideas on what I can do to make this work?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/257923-help-with-invalid-characters/
Share on other sites

I know this is solved but I thought I would give my tuppence worth. You could have used \W to match anything that is not a word character. Just remember this, all the shorthand regex tricks you know, \w \d \s etc etc., they all have exact opposites which are there uppercase versions. I.e.:

 

\W = Match any character that is not a word character

\D = Match any character that is not a digit

\S =  Match any character that is not a white space character etc etc.

 

Make those letters lower case and you need only to remove the word not from the definitions to find they work.

 

Furthermore, you also amusingly almost posted a regex which needed extremely little tweaking to have worked. You were just missing the vital ^ character. Placed within a character class it means match anything that is not the following characters. So you're regex, [a-zA-Z0-9], needed only become, [^a-zA-Z0-9], to have worked.

 

Joe

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.