Regex help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nicholas Sherlock

    Regex help

    Hey there,

    I'm trying to get a Perl regular expression to work, and I can't seem to
    get it quite right.

    It should match text like [download: 12,20,28] and match the subpatterns
    "12", ",20", ",28".

    /\[download:[^0-9]*([0-9]+)(,[0-9]+)*\]/i

    When I try it, it only returns the first and last items (Ie. "12",
    ",28"). What am I missing?

    Cheers,
    Nicholas Sherlock
  • d

    #2
    Re: Regex help

    "Nicholas Sherlock" <N.sherlock@gma il.com> wrote in message
    news:drvbnp$uhu $1@lust.ihug.co .nz...[color=blue]
    > Hey there,
    >
    > I'm trying to get a Perl regular expression to work, and I can't seem to
    > get it quite right.
    >
    > It should match text like [download: 12,20,28] and match the subpatterns
    > "12", ",20", ",28".
    >
    > /\[download:[^0-9]*([0-9]+)(,[0-9]+)*\]/i
    >
    > When I try it, it only returns the first and last items (Ie. "12", ",28").
    > What am I missing?
    >
    > Cheers,
    > Nicholas Sherlock[/color]

    Your pattern is trying to recursively match patterns, which is a grey area
    according to the php manual at least. May I ask why you're using pattern
    matching for this? You could just use:

    $text="[download: 12,20,28]";
    $id=explode("," , substr($text, 11, -1));

    That would give you an array of 12, 20, 28, and without pattern matching...

    dave


    Comment

    • David  Wahler

      #3
      Re: Regex help

      Nicholas Sherlock wrote:[color=blue]
      > Hey there,
      >
      > I'm trying to get a Perl regular expression to work, and I can't seem to
      > get it quite right.
      >
      > It should match text like [download: 12,20,28] and match the subpatterns
      > "12", ",20", ",28".
      >
      > /\[download:[^0-9]*([0-9]+)(,[0-9]+)*\]/i
      >
      > When I try it, it only returns the first and last items (Ie. "12",
      > ",28"). What am I missing?[/color]

      The details are in the reference
      <http://us3.php.net/manual/en/reference.pcre. pattern.syntax. php>,
      they're just not always easy to find.

      The paragraph you're looking for is at the bottom of the "Repetition "
      section:

      (quote)
      When a capturing subpattern is repeated, the value captured is the
      substring that matched the final iteration. For example, after
      (tweedle[dume]{3}\s*)+ has matched "tweedledum tweedledee" the value
      of the captured substring is "tweedledee ". However, if there are nested
      capturing subpatterns, the corresponding captured values may have been
      set in previous iterations. For example, after /(a|(b))+/ matches
      "aba" the value of the second captured substring is "b".
      (end quote)

      So, it doesn't look like you can use regular expressions to split a
      string like that. If you need to split based on a real regular
      expression rather than a fixed string, check out pcre_split.

      -- David

      Comment

      • David  Wahler

        #4
        Re: Regex help


        David Wahler wrote:[color=blue]
        > So, it doesn't look like you can use regular expressions to split a
        > string like that. If you need to split based on a real regular
        > expression rather than a fixed string, check out pcre_split.[/color]

        Sorry, that should be preg_split.

        -- David

        Comment

        • Nicholas Sherlock

          #5
          Re: Regex help

          David Wahler wrote:[color=blue]
          > Nicholas Sherlock wrote:[color=green]
          >> /\[download:[^0-9]*([0-9]+)(,[0-9]+)*\]/i
          >>
          >> When I try it, it only returns the first and last items (Ie. "12",
          >> ",28"). What am I missing?[/color]
          >
          > (quote)
          > When a capturing subpattern is repeated, the value captured is the
          > substring that matched the final iteration. [...]
          > (end quote)
          >
          > So, it doesn't look like you can use regular expressions to split a
          > string like that. If you need to split based on a real regular
          > expression rather than a fixed string, check out pcre_split.[/color]

          Thanks, I didn't realize my pattern would be recursive/repetitive. As I
          don't really need to get each argument separately, I changed it to:

          /\[download:[^0-9]*([0-9]+(,[0-9]+)*)+\]/i

          Which returns for me the whole 1,2,3,4 block as a matched pattern,
          perfect :).

          Cheers,
          Nicholas Sherlock

          Comment

          • Jim Michaels

            #6
            Re: Regex help


            "Nicholas Sherlock" <N.sherlock@gma il.com> wrote in message
            news:ds0r17$kqt $1@lust.ihug.co .nz...[color=blue]
            > David Wahler wrote:[color=green]
            >> Nicholas Sherlock wrote:[color=darkred]
            >>> /\[download:[^0-9]*([0-9]+)(,[0-9]+)*\]/i
            >>>
            >>> When I try it, it only returns the first and last items (Ie. "12",
            >>> ",28"). What am I missing?[/color]
            >>
            >> (quote)
            >> When a capturing subpattern is repeated, the value captured is the
            >> substring that matched the final iteration. [...]
            >> (end quote)
            >>
            >> So, it doesn't look like you can use regular expressions to split a
            >> string like that. If you need to split based on a real regular
            >> expression rather than a fixed string, check out pcre_split.[/color]
            >
            > Thanks, I didn't realize my pattern would be recursive/repetitive. As I
            > don't really need to get each argument separately, I changed it to:
            >
            > /\[download:[^0-9]*([0-9]+(,[0-9]+)*)+\]/i[/color]

            can shorten it here:
            /\[download:[^\d]*([\d]+(,[\d]+)*)+\]/i

            [color=blue]
            >
            > Which returns for me the whole 1,2,3,4 block as a matched pattern, perfect
            > :).
            >
            > Cheers,
            > Nicholas Sherlock[/color]


            Comment

            • Jim Michaels

              #7
              Re: Regex help


              "Jim Michaels" <jmichae3@nospa m.yahoo.com> wrote in message
              news:pZadnZ-Oo9z0c3PenZ2dnU VZ_sSdnZ2d@comc ast.com...[color=blue]
              >
              > "Nicholas Sherlock" <N.sherlock@gma il.com> wrote in message
              > news:ds0r17$kqt $1@lust.ihug.co .nz...[color=green]
              >> David Wahler wrote:[color=darkred]
              >>> Nicholas Sherlock wrote:
              >>>> /\[download:[^0-9]*([0-9]+)(,[0-9]+)*\]/i
              >>>>
              >>>> When I try it, it only returns the first and last items (Ie. "12",
              >>>> ",28"). What am I missing?
              >>>
              >>> (quote)
              >>> When a capturing subpattern is repeated, the value captured is the
              >>> substring that matched the final iteration. [...]
              >>> (end quote)
              >>>
              >>> So, it doesn't look like you can use regular expressions to split a
              >>> string like that. If you need to split based on a real regular
              >>> expression rather than a fixed string, check out pcre_split.[/color]
              >>
              >> Thanks, I didn't realize my pattern would be recursive/repetitive. As I
              >> don't really need to get each argument separately, I changed it to:
              >>
              >> /\[download:[^0-9]*([0-9]+(,[0-9]+)*)+\]/i[/color]
              >
              > can shorten it here:
              > /\[download:[^\d]*([\d]+(,[\d]+)*)+\]/i[/color]

              oops... maybe it more like
              /\[download:[^\d]*(\d+(,\d+)*)+\]/i

              [color=blue]
              >
              >[color=green]
              >>
              >> Which returns for me the whole 1,2,3,4 block as a matched pattern,
              >> perfect :).
              >>
              >> Cheers,
              >> Nicholas Sherlock[/color]
              >
              >[/color]


              Comment

              • Nicholas Sherlock

                #8
                Re: Regex help

                Jim Michaels wrote:[color=blue]
                > oops... maybe it more like
                > /\[download:[^\d]*(\d+(,\d+)*)+\]/i[/color]

                Thanks, very useful to know.

                Cheers,
                Nicholas Sherlock

                Comment

                Working...