2 Replies Latest reply on Jul 26, 2007 2:36 PM by 807605

    How to make regex alternation return first match?

      I have the following string:

      "i am a man and i have a cat and a dog and a bird"

      and I wish to extract the part of the sentence from "man" up to the first occurence of "dog" or "cat". So I wish the extracted sentence to be

      "and i have a"

      or alternatively

      "man and i have a cat"

      I would guess the regular expression "man.*(cat|dog)" should do it, but that seems to always return the sentence up to the last occurrence of dog and cat, when I want the first occurrence. So "man.*(cat|dog)" returns (when using find() and group()):

      "man and i have a cat and a dog"

      Anyone have any ideas how I can return the sentence from (preferably excluding) man and to (preferably excluding) the first occurrence of cat or dog?


      Message was edited by:

      Message was edited by: