2 Replies Latest reply: Jul 26, 2007 9:36 AM by 807605 RSS

    How to make regex alternation return first match?

    807605
      Hi,
      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?

      Thanks!

      Message was edited by:
      MagnusGJ

      Message was edited by:
      MagnusGJ