Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts

    Simple Regex Question

    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:

    Code:
    [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 . 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).
    Last edited by binaryWeapon; 08-12-2008 at 03:45 AM.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    use regex or |
    Code:
    [0-9A-Za-z_]([=><]|!=)[0-9A-Za-z_]
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by binaryWeapon View Post
    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:

    Code:
    [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 . 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
    Code:
    ^\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?
    Code:
    ^\w+\s*(=|==|<=|<|>=|>|!=)
    and another word
    same words, but with spaces before and don't forget $
    Code:
    ^\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

  • Users who have thanked oesxyl for this post:

    binaryWeapon (08-11-2008)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •