Announcement

Collapse
No announcement yet.

Regular Expression Help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Regular Expression Help

    Hi all,

    Any regex experts out there? Here's what I'm trying to do:

    The regex needs to match when:

    - A document must contain the word Black and must also contain the word Blue, but cannot contain the word Green. The three words could be in any order, the document contains multiple lines and case is important.

    Any ideas? Been banging my head against this for a while. Found a few ways that I thought worked, but more testing found that they didn't work in all cases.

    Thanks
    Al
    HS 4.2.8.0: 2134 Devices 1252 Events
    Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

    #2
    Originally posted by sparkman View Post
    Hi all,

    Any regex experts out there? Here's what I'm trying to do:

    The regex needs to match when:

    - A document must contain the word Black and must also contain the word Blue, but cannot contain the word Green. The three words could be in any order, the document contains multiple lines and case is important.

    Any ideas? Been banging my head against this for a while. Found a few ways that I thought worked, but more testing found that they didn't work in all cases.

    Thanks
    Al

    I am not certain what you mean by document nor which language you are using so I can't give you the syntax. However, generically, keep it simple.

    if document contains 'Black' and document contains 'blue' and document does not contain 'green' should work.

    As I only program in Perl anymore, for example:

    Code:
    $a = "Blue is not Black but sort \nof Greeeeeeeen";
    
    if ($a =~ m/black/i && $a =~ m/blue/i && $a !~ m/green/i) {
            print "got it.\n";
    } else {
            print "nope.\n";
    }
    Len


    HomeSeer Version: HS3 Pro Edition 3.0.0.435
    Linux version: Linux homeseer Ubuntu 16.04 x86_64
    Number of Devices: 633
    Number of Events: 773

    Enabled Plug-Ins
    2.0.54.0: BLBackup
    2.0.40.0: BLLAN
    3.0.0.48: EasyTrigger
    30.0.0.36: RFXCOM
    3.0.6.2: SDJ-Health
    3.0.0.87: weatherXML
    3.0.1.190: Z-Wave

    Comment


      #3
      Originally posted by lveatch View Post
      I am not certain what you mean by document nor which language you are using so I can't give you the syntax. However, generically, keep it simple.

      if document contains 'Black' and document contains 'blue' and document does not contain 'green' should work.

      As I only program in Perl anymore, for example:

      Code:
      $a = "Blue is not Black but sort \nof Greeeeeeeen";
      
      if ($a =~ m/black/i && $a =~ m/blue/i && $a !~ m/green/i) {
              print "got it.\n";
      } else {
              print "nope.\n";
      }
      Thanks for the response! By document, I meant a multi-line string, not just a single line. I'm looking for the vb.net syntax, specifically for a regex that can be used in the received email event trigger.

      Thanks
      Al
      HS 4.2.8.0: 2134 Devices 1252 Events
      Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

      Comment


        #4
        I'm not good with Regex but I've had a stab at this:
        PHP Code:
        ^(?!.*\bgreen\b)(?=.*blue)(?=.*black).*$ 
        Jon

        Comment


          #5
          Originally posted by jon00 View Post
          I'm not good with Regex but I've had a stab at this:
          PHP Code:
          ^(?!.*\bgreen\b)(?=.*blue)(?=.*black).*$ 
          Thanks Jon, that looks to be working after I added (?s) to the front of it (so that the . character also matches on newlines). Any pros and cons of having the \b around green, but not the other two words?

          Thanks
          Al
          HS 4.2.8.0: 2134 Devices 1252 Events
          Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

          Comment


            #6
            I expect either will work fine.
            Jon

            Comment


              #7
              Originally posted by jon00 View Post
              I expect either will work fine.
              Thanks!
              HS 4.2.8.0: 2134 Devices 1252 Events
              Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

              Comment


                #8
                Without the boundary delimiters (\b), for example 'blues' would match but with them it wouldn't.

                Comment


                  #9
                  Originally posted by zwolfpack View Post
                  Without the boundary delimiters (\b), for example 'blues' would match but with them it wouldn't.
                  Great, thanks for the clarification!
                  HS 4.2.8.0: 2134 Devices 1252 Events
                  Z-Wave 3.0.10.0: 133 Nodes on one Z-Net

                  Comment

                  Working...
                  X