in reply to Regular Expression To Extract Multiple Matches Pattern

If you're replacing the string you're searching for, there's no need to use m/// at all ...

while ( $teststring =~ s/\b([a-z]+-[a-z]+-[a-z]+)\b/phrase/i ) { print $1; }

    --k.


Replies are listed 'Best First'.
Re: Re: Regular Expression To Extract Multiple Matches Pattern
by talexb (Chancellor) on Jan 07, 2002 at 19:37 UTC
    Original Post:

    I was working on a regular expression today to extract phrases like "not-too-shabby" and "never-before-seen",. that is, three words separated by hyphens

    The original post does talk about 'extracting' not 'replacing'. And something like print $1; always raises a red flag for me -- you usually want print "$1\n"; instead, otherwise the doubly hyphenated words that the script has found will run into each other -- probably not what you want.

    --t. alex

    "Excellent. Release the hounds." -- Monty Burns.

      Further on down in the post, Cody says ...

      ...but surely there's a smarter way to do it than check there's a match, then extract it with one regex, then remove it from the test string with another?

      Perhaps I'm reading the bolded part of that wrong, but it sounds like replacing to me; a notion reinforced by the code snippet given.

      The $1 was an oversight, though: an artifact of using -l in my shebang. ;)

          --k.


        Hi, thank you all for your help.

        The regex I put in there is not exactly the regex I used, as I said, the regex was not the point of the exercise.

        It all seems so simple now that I know I can do a "while" like that.

Re^2: Regular Expression To Extract Multiple Matches Pattern
by Aristotle (Chancellor) on Jan 07, 2002 at 17:49 UTC
    And certainly those parts you left out from s/(^.* )([a-z]+-[a-z#93;+-[a-z#93;+)( .*$)/\1 phrase \3/gi; look hideously ugly.