How can I improve this regex ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    #1

    How can I improve this regex ?

    Hi,

    I am not sure if this can be solved with regex,
    possibly the string needs to be chopped into words
    and then stepped through ( but not sure how).

    Anyway, this is what I have and it is very close to what
    I want.:
    Code:
    preg_match_all("#((?:\b\w{1,20}\b\s+){2})#", $data, $matches)
    Here is part of the out put from print_r($matche s);

    [32] => technical support [33] => services attempt [34] => to help
    [35] => the user [36] => solve specific [37] => problems with

    As you can see, the data is just being divided into two word chunks.

    And I am missing half of the possible phrases eg "support services"
    is not reported.

    This is not quite what I expected

    What I wanted was a list of all the two word phrases,
    so I should be getting:

    [32] => technical support [33] => support services [34] => services attempt
    [35] => attempt to [36] => to help [37] => help the [38] => the user

    You see the overlap ?
    This ensures that I do get all the phrases.

    Any ideas on how would I need to change my
    regex to achieve my desired output ?

    If not possible, how else can I achieve it ?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    there’s nothing you can do while using preg_match_all( )

    Originally posted by php.net
    After the first match is found, the subsequent searches are continued on from end of the last match.

    Comment

    • jeddiki
      Contributor
      • Jan 2009
      • 290

      #3
      So I think the "b" part of the question comes into play.

      Any suggestions ?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        \b = word boundary

        otherwise see above quote

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Hey.

          I don't see a way to do this using regexp alone. It just searches for patterns, it doesn't do logic.

          You could just split the string into induvidual words and have PHP pair the together, two and two.
          A loop that goes through each word in the array, partnering it up with the next word in the list, added to a second array.

          [code=php]
          $words = explode(' ', $input);
          for($i = 1; $i < count($words) - 1; ++$i) {
          $pairs[] = $words[$i-1] . " " . $words[$i];
          }[/code]

          Comment

          Working...