regular expression - help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kaptain kernel

    regular expression - help

    can anyone translate this into plain english?

    preg_match_all( "/(\w+[,. ?])+/U", $text, $words);


  • Eric Kincl

    #2
    Re: regular expression - help

    kaptain kernel wrote:
    [color=blue]
    > can anyone translate this into plain english?
    >
    > preg_match_all( "/(\w+[,. ?])+/U", $text, $words);[/color]




    -Eric Kincl

    Comment

    • Justin Koivisto

      #3
      Re: regular expression - help

      kaptain kernel wrote:
      [color=blue]
      > can anyone translate this into plain english?
      >
      > preg_match_all( "/(\w+[,. ?])+/U", $text, $words);
      >
      >[/color]

      /(\w+[,. ?])+/U

      \w+ : word character, one or more times

      [,. ?] : (looks like) a literal comman, period, space, or question mark

      (\w+[,. ?])+ : the above explanations, one or more times

      /U : ungreedy (doesn't effect this pattern)

      See here for more info:

      Perl regex, regular expression, extracting information, text processing



      --
      Justin Koivisto - spam@koivi.com
      PHP POSTERS: Please use comp.lang.php for PHP related questions,
      alt.php* groups are not recommended.

      Comment

      • John Dunlop

        #4
        Re: regular expression - help

        Justin Koivisto wrote:
        [color=blue][color=green]
        > > preg_match_all( "/(\w+[,. ?])+/U", $text, $words);[/color]
        >
        > /U : ungreedy (doesn't effect this pattern)[/color]

        I had thought that initially too.

        The pattern has one capturing subpattern, meaning -- with the
        default flag for preg_match_all (PREG_PATTERN_O RDER), which
        applies in this case as no flag was explicitly specified -- that
        the $words array contains two further arrays: one array containing
        full pattern matches, and another array containing the capturing
        subpattern matches.

        But because the U modifier (PCRE_UNGREEDY) is set, both arrays
        will have exactly the same contents; that is, they'll both contain
        values of one or more word characters followed by a single comma,
        period, space, or question mark. That seems redundant to me.

        I wonder what this pattern is supposed to accomplish.

        --
        Jock

        Comment

        • Andy Hassall

          #5
          Re: regular expression - help

          On Mon, 10 Nov 2003 17:16:47 +0000, kaptain kernel <nospam@nospam. gov> wrote:
          [color=blue]
          >can anyone translate this into plain english?
          >
          >preg_match_all ("/(\w+[,. ?])+/U", $text, $words);[/color]

          /U isn't a Perl regex modifier, manual for PCRE says it makes matches
          non-greedy by default though.

          For the rest, YAPE::Regex::Ex plain (a useful Perl module) comes up with:
          (after appending '?' to all the quantifiers to make them non-greedy)

          The regular expression:

          (?-imsx:(\w+?[,. ?])+?)

          matches as follows:

          NODE EXPLANATION
          ----------------------------------------------------------------------
          (?-imsx: group, but do not capture (case-sensitive)
          (with ^ and $ matching normally) (with . not
          matching \n) (matching whitespace and #
          normally):
          ----------------------------------------------------------------------
          ( group and capture to \1 (1 or more times
          (matching the least amount possible)):
          ----------------------------------------------------------------------
          \w+? word characters (a-z, A-Z, 0-9, _) (1 or
          more times (matching the least amount
          possible))
          ----------------------------------------------------------------------
          [,. ?] any character of: ',', '.', ' ', '?'
          ----------------------------------------------------------------------
          )+? end of \1 (NOTE: because you're using a
          quantifier on this capture, only the LAST
          repetition of the captured pattern will be
          stored in \1)
          ----------------------------------------------------------------------
          ) end of grouping
          ----------------------------------------------------------------------

          --
          Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
          Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

          Comment

          Working...