adam84 Posted February 28, 2012 Share Posted February 28, 2012 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 Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 28, 2012 Share Posted February 28, 2012 There's no need for a regex pattern for that. Use ctype_alnum. Quote Link to comment Share on other sites More sharing options...
ragax Posted March 2, 2012 Share Posted March 2, 2012 Pikachu is right, but just for the record, a regex version: <?php $regex=',[^[:alnum:]],'; echo preg_match($regex,"almost_alnum"); ?> The output is 1 because of the underscore in almost_alnum. Quote Link to comment Share on other sites More sharing options...
joe92 Posted March 3, 2012 Share Posted March 3, 2012 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.