Little REGEXP question

Discussion in 'PHP' started by SEOAnalytic.com, Oct 27, 2008.

  1. #1
    I have this
    
    preg_match_all("#findCriteria\((.*) , nkView\.MATCH_THIS#Usi",$string,$matches);
    
    Code (markup):
    The code above doesnt work as expected. I think the problem is somewhere with the spaces before and after the comma. If i replace those spaces with (?:\s) too many unwanted results would be returned. So what to do with those spaces?
     
    SEOAnalytic.com, Oct 27, 2008 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What exactly are you trying to match against?
     
    JAY6390, Oct 27, 2008 IP
  3. SEOAnalytic.com

    SEOAnalytic.com Member

    Messages:
    106
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #3
    i have for example the string 'findCriteria("something", "something_else" , nkView.MATCH_THIS)' .
    i need to take out '"something", "something_else"' . I don't know how to represend those two spaces in regexp.
     
    SEOAnalytic.com, Oct 27, 2008 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    /findCriteria\((.*?) , nkView\.MATCH_THIS\)/
    That works for me
     
    JAY6390, Oct 27, 2008 IP
  5. SEOAnalytic.com

    SEOAnalytic.com Member

    Messages:
    106
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #5
    It doesnt work for
    
    $str='criteria.push(new findCriteria(\'[something]\', 9.046619415283203, \'90,500\', \'90,500\', 90500, 90500, 1.0 , \'$1.14\', 1140330, \'1 - 3\', 2 , 0 , 0 , varyMon, 9 , \'\' , nkView.MATCH_THIS ));';
    
    Code (markup):
     
    SEOAnalytic.com, Oct 27, 2008 IP
  6. SEOAnalytic.com

    SEOAnalytic.com Member

    Messages:
    106
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #6
    Oops, sorry it works for my last post. But it doesn work if the string contains more than 1 line.
     
    SEOAnalytic.com, Oct 27, 2008 IP
  7. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #7
    Be sure to add the s (lowercase) flag to the pattern.

     
    joebert, Oct 27, 2008 IP
  8. SEOAnalytic.com

    SEOAnalytic.com Member

    Messages:
    106
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #8
    Ok, thanks to all for helping me.
    Now i have another question.
    How do i match everything until a word nkView ?
    I tried ((^[nkView])*) . Is it correct?
     
    SEOAnalytic.com, Oct 28, 2008 IP
  9. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #9
    A common misconception about the character class ( [...] or [^...] ) is that the characters within it are in sequence. This is wrong, an easy way to remember it is that a character class is a big list of characters with | (or) flags between them.

    The following two patterns are effectively the same.

    [aceg]
    Code (markup):
    a|c|e|g
    Code (markup):
    Check out the posts about Lookahead Assertions in this recent thread.
    http://forums.digitalpoint.com/showthread.php?t=1079610

    In your case the pattern might look like this.

    #findcriteria(.+(?!kview))nkview#i
    Code (markup):
    That translates to "match findcriteria, followed by anything one or more times as long as the anything is not followed by kview, followed by nkview".
     
    joebert, Oct 28, 2008 IP
  10. SEOAnalytic.com

    SEOAnalytic.com Member

    Messages:
    106
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #10
    Thanks for pointing to that resource. It really helped me.
    A little improvement to suit my needs was to modify your regex like this:
    
    #findcriteria(.(?!kview))*?nkview#si
    
    Code (markup):
     
    SEOAnalytic.com, Oct 28, 2008 IP