Want regular expression to stop at first occurrence of word

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • e_matthes@hotmail.com

    #1

    Want regular expression to stop at first occurrence of word

    Hello all,

    Say I have the following string:

    $string = "list of whales: white beluga whale humpback whale atlantic
    humpback whale";

    I want to pull out the first kind of whale (white beluga). I want
    this to work regardless of whether the first whale is a white beluga,
    or a humpback, etc. I tried:

    $regExp = "/(whales:)(.*)(w hale)/";
    $outputArray = array();
    if ( preg_match($reg Exp, $string, $outputArray) ) {
    print "$outputArr ay[2]<br>";
    }

    But, the output is "white beluga whale humpback whale atlantic
    humpback", as I expected. I know how to stop after finding a single
    character, but I can't figure out how to stop after finding a single
    instance of a word. Any help? Thanks.

  • Heiko Richler

    #2
    Re: Want regular expression to stop at first occurrence of word

    e_matthes@hotma il.com wrote:
    $string = "list of whales: white beluga whale humpback whale atlantic
    humpback whale";
    $regExp = "/(whales:)(.*)(w hale)/";
    if ( preg_match($reg Exp, $string, $outputArray) ) {
    But, the output is "white beluga whale humpback whale atlantic
    humpback", as I expected. I know how to stop after finding a single
    character, but I can't figure out how to stop after finding a single
    instance of a word. Any help? Thanks.
    RegEx are greedy. That means RegEx match as much as they can. This means
    your Expression stops with the last whale. To make it stop with the
    first whale make your Expression ungreedey:


    $regExp = "/(whales:)(.*?)( whale)/";

    or

    $regExp = "/(whales:)(.*)(w hale)/U";

    see:


    Heiko
    --
    http://portal.richler.de/ Namensportal zu Richler
    http://www.richler.de/ Heiko Richler: Computer - Know How!
    http://www.richler.info/ private Homepage

    Comment

    • e_matthes@hotmail.com

      #3
      Re: Want regular expression to stop at first occurrence of word

      On Feb 25, 10:25 pm, Heiko Richler <heiko-rich...@nefkom. netwrote:
      e_matt...@hotma il.com wrote:
      $string = "list of whales: white beluga whale humpback whale atlantic
      humpback whale";
      $regExp = "/(whales:)(.*)(w hale)/";
      if ( preg_match($reg Exp, $string, $outputArray) ) {
      But, the output is "white beluga whale humpback whale atlantic
      humpback", as I expected. I know how to stop after finding a single
      character, but I can't figure out how to stop after finding a single
      instance of a word. Any help? Thanks.
      >
      RegEx are greedy. That means RegEx match as much as they can. This means
      your Expression stops with the last whale. To make it stop with the
      first whale make your Expression ungreedey:
      >
      $regExp = "/(whales:)(.*?)( whale)/";
      >
      or
      >
      $regExp = "/(whales:)(.*)(w hale)/U";
      >
      see:http://en.wikipedia.org/wiki/Regular...dy_expressions
      >
      Heiko
      --http://portal.richler. de/Namensportal zu Richlerhttp://www.richler.de/ Heiko Richler: Computer - Know How!http://www.richler.info/ private Homepage
      Thank you very much, both for the answer and the reference.

      Eric

      Comment

      Working...