...

View Full Version : Resolved Simple Regex Question



binaryWeapon
08-11-2008, 04:08 PM
I am in the midst of learning regex and have run into a simple (I hope) question that I cannot seem to find on the net. I have the regex:


[0-9A-Za-z_][=><][0-9A-Za-z_]

It checks for a word, a comparing sign (=, >, <, etc), and another word. I want to add the sign != to the comparing signs, but obviously putting two characters interprets them as 2 different ones. How can I do this? Also, is this the most efficient way to accomplish what I'm trying to do? This is my first experience actually using regular expressions in code so I don't want to get anything wrong :p. I need it to check the whole string, not just part of it (ie if there was a character, a comparing sign, and another character in the middle of the string, but other characters outside, like "*$*^$a>b*)&#$&^", it shouldn't work anymore).

shyam
08-11-2008, 05:15 PM
use regex or |

[0-9A-Za-z_]([=><]|!=)[0-9A-Za-z_]

oesxyl
08-11-2008, 05:55 PM
I am in the midst of learning regex and have run into a simple (I hope) question that I cannot seem to find on the net. I have the regex:


[0-9A-Za-z_][=><][0-9A-Za-z_]

Build it step by step from left to right. :)
First of all is a little difference between regex for different preg function so you must first decide which one you want to use.
Second, build test cases, :). That means take a string, relevant for your problem and work on it.


It checks for a word, a comparing sign (=, >, <, etc), and another word. I want to add the sign != to the comparing signs, but obviously putting two characters interprets them as 2 different ones. How can I do this? Also, is this the most efficient way to accomplish what I'm trying to do? This is my first experience actually using regular expressions in code so I don't want to get anything wrong :p. I need it to check the whole string, not just part of it (ie if there was a character, a comparing sign, and another character in the middle of the string, but other characters outside, like "*$*^$a>b*)&#$&^", it shouldn't work anymore).
from what you said:

I need it to check the whole string, not just part of it
that means your regex start with ^ and end with $, keep in mind $

It checks for a word,
usualy this is for shor \w+, word char one or more

^\w+

a comparing sign (=, >, <, etc)
usualy people use to put spaces between, but could be without, \s*, spaces zero or more
another problem are relation like <= or >=, how you deal with this? ignore or not?

^\w+\s*(=|==|<=|<|>=|>|!=)

and another word
same words, :) but with spaces before and don't forget $

^\w+\s*(=|==|<=|<|>=|>|!=)\s*\w+$

I don't know if this work for you, but this is the idea, :)

PS: if you can solve a problem without regex do it, :)

regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum