Simple Regular Expression ?

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

    #1

    Simple Regular Expression ?

    I would like a regex for the following html snippet:

    <h2>Field1</h2>

    <h3>

    $123,456.78 - $987,654.32

    </h3>

    I would like to capture Field1 and the first numeric value only.

    I have created the following that works somewhat:
    $pattern='%<h2> (?P<field1>.*?) </h2>
    .*?
    <h3>.*?\$(?P<fi eld2>.*?)\s.*?</h3>
    %six';
    However I would like to improve field2's capture to be the first series of
    numbers after <h3> excluding the thousand seperator and stop the capture as
    soon as a non numeric is encountered other than the decimal point, I cannot
    depend on the dollar sign always being present, so in this case I'd capture
    123456.78

    Thanks in advance...



  • Rik

    #2
    Re: Simple Regular Expression ?

    McHenry wrote:[color=blue]
    > <h2>Field1</h2>
    >
    > <h3>
    >
    > $123,456.78 - $987,654.32
    >
    > </h3>
    >
    > I would like to capture Field1 and the first numeric value only.
    > I have created the following that works somewhat:
    > $pattern='%<h2> (?P<field1>.*?) </h2>
    > .*?[/color]
    [color=blue]
    >
    > <h3>.*?\$(?P<fi eld2>.*?)\s.*?</h3> %six'; However I would like to
    > improve field2's capture to be the first series of numbers after <h3>
    > excluding the thousand seperator and stop the capture as soon as a
    > non numeric is encountered other than the decimal point, I cannot
    > depend on the dollar sign always being present, so in this case I'd
    > capture 123456.78
    >
    > Thanks in advance...[/color]

    simple one, capture at least 1 number, fo9llowed by numbers, decimal- or
    thousand-seperator:
    <h3>.*?(?P<fiel d2>[0-9]+[0-9\.,]*).*?</h3>

    advanced, will validate currency format:
    <h3>.*?(?P<fiel d2>(?:[1-9][0-9]{0,2}(?:,[0-9]{3})*|0)(?:\.[0-9]{2})?).*?</h3[color=blue]
    >[/color]

    allow for unexpected html tags/attributes, where we don't want to match the
    '10' in a '<span margin="10px">' for instance:
    <h3[^>]*>(?:[^<]*?(?:<[^>]*>)?)*?(?P<fiel d2>(?:[1-9][0-9]{0,2}(?:,[0-9]{3})*
    |0)(?:\.[0-9]{2})?).*?</h3>

    Offcourse, if you're naming your captures 'field1' & 'field2', you might as
    well not name them at all.

    Grtz,
    --
    Rik Wasmus


    Comment

    • McHenry

      #3
      Re: Simple Regular Expression ?


      "Rik" <luiheidsgoeroe @hotmail.com> wrote in message
      news:d309d$449f d595$8259c69c$5 932@news1.tudel ft.nl...[color=blue]
      > McHenry wrote:[color=green]
      >> <h2>Field1</h2>
      >>
      >> <h3>
      >>
      >> $123,456.78 - $987,654.32
      >>
      >> </h3>
      >>
      >> I would like to capture Field1 and the first numeric value only.
      >> I have created the following that works somewhat:
      >> $pattern='%<h2> (?P<field1>.*?) </h2>
      >> .*?[/color]
      >[color=green]
      >>
      >> <h3>.*?\$(?P<fi eld2>.*?)\s.*?</h3> %six'; However I would like to
      >> improve field2's capture to be the first series of numbers after <h3>
      >> excluding the thousand seperator and stop the capture as soon as a
      >> non numeric is encountered other than the decimal point, I cannot
      >> depend on the dollar sign always being present, so in this case I'd
      >> capture 123456.78
      >>
      >> Thanks in advance...[/color]
      >[/color]

      Rik I started a new thread as I had asked you enough and didn't want to push
      your generosity, having said this I am glad you responded, thanks.
      [color=blue]
      > simple one, capture at least 1 number, fo9llowed by numbers, decimal- or
      > thousand-seperator:
      > <h3>.*?(?P<fiel d2>[0-9]+[0-9\.,]*).*?</h3>[/color]

      I'll stick to this one as the ones below are over my head...

      Why could we not simply have used as this is what I tried and it didn't work
      ?
      <h3>.*?(?P<fiel d2>[0-9\.,]*).*?</h3>

      [color=blue]
      >
      > advanced, will validate currency format:
      > <h3>.*?(?P<fiel d2>(?:[1-9][0-9]{0,2}(?:,[0-9]{3})*|0)(?:\.[0-9]{2})?).*?</h3[color=green]
      >>[/color]
      >
      > allow for unexpected html tags/attributes, where we don't want to match
      > the
      > '10' in a '<span margin="10px">' for instance:
      > <h3[^>]*>(?:[^<]*?(?:<[^>]*>)?)*?(?P<fiel d2>(?:[1-9][0-9]{0,2}(?:,[0-9]{3})*
      > |0)(?:\.[0-9]{2})?).*?</h3>
      >
      > Offcourse, if you're naming your captures 'field1' & 'field2', you might
      > as
      > well not name them at all.[/color]

      This was simply to help illustrate where the fields were in the regex
      [color=blue]
      >
      > Grtz,
      > --
      > Rik Wasmus
      >
      >[/color]


      Comment

      • Rik

        #4
        Re: Simple Regular Expression ?

        McHenry wrote:[color=blue]
        > Why could we not simply have used as this is what I tried and it
        > didn't work ?
        > <h3>.*?(?P<fiel d2>[0-9\.,]*).*?</h3>[/color]

        1. It matches a single dot or comma, not desired.
        2. It matches 'nothing' (* = 0 or more)

        In <h3>.*?(?P<fiel d2>[0-9]+[0-9\.,]*).*?</h3>, we use [0-9]+ to say: once
        you have found at least 1 number, and maybe more, capture all numbers,
        comma's and dot's.

        Grtz,
        --
        Rik Wasmus


        Comment

        • McHenry

          #5
          Re: Simple Regular Expression ?


          "Rik" <luiheidsgoeroe @hotmail.com> wrote in message
          news:ab90a$449f de69$8259c69c$1 0096@news1.tude lft.nl...[color=blue]
          > McHenry wrote:[color=green]
          >> Why could we not simply have used as this is what I tried and it
          >> didn't work ?
          >> <h3>.*?(?P<fiel d2>[0-9\.,]*).*?</h3>[/color]
          >
          > 1. It matches a single dot or comma, not desired.
          > 2. It matches 'nothing' (* = 0 or more)
          >
          > In <h3>.*?(?P<fiel d2>[0-9]+[0-9\.,]*).*?</h3>, we use [0-9]+ to say: once
          > you have found at least 1 number, and maybe more, capture all numbers,
          > comma's and dot's.
          >
          > Grtz,
          > --
          > Rik Wasmus
          >
          >[/color]

          Thanks Rik, I understand the difference... wow are these things normally
          easy to follow or always this heard ?

          I have a regex, that performs three captures of three prices, it works when
          all three are present and numeric however if one is missing or listed as POA
          or similar then all three fail. Can this be overcome or do I need three
          seperate preg_match statements ?

          Thanks in advance...


          Comment

          • Rik

            #6
            Re: Simple Regular Expression ?

            McHenry wrote:[color=blue]
            > "Rik" <luiheidsgoeroe @hotmail.com> wrote in message
            > news:ab90a$449f de69$8259c69c$1 0096@news1.tude lft.nl...[color=green]
            >> McHenry wrote:[color=darkred]
            >>> Why could we not simply have used as this is what I tried and it
            >>> didn't work ?
            >>> <h3>.*?(?P<fiel d2>[0-9\.,]*).*?</h3>[/color]
            >>
            >> 1. It matches a single dot or comma, not desired.
            >> 2. It matches 'nothing' (* = 0 or more)
            >>
            >> In <h3>.*?(?P<fiel d2>[0-9]+[0-9\.,]*).*?</h3>, we use [0-9]+ to say:
            >> once you have found at least 1 number, and maybe more, capture all
            >> numbers, comma's and dot's.[/color]
            >
            > Thanks Rik, I understand the difference... wow are these things
            > normally easy to follow or always this heard ?[/color]

            YOu'll just have to get used to it, the more you use them, the easier they
            become. One reason I normally reply to regex questions is to sharpen my
            skills :-).
            [color=blue]
            > I have a regex, that performs three captures of three prices, it
            > works when all three are present and numeric however if one is
            > missing or listed as POA or similar then all three fail. Can this be
            > overcome or do I need three seperate preg_match statements ?
            >
            > Thanks in advance...[/color]

            With listed as 'POA' you mean it literally?
            If you're using the simple one:

            <h3>.*?(?:(?P<p rices>(?:[0-9]+[0-9\.,]*)|POA).*?){1,3 }</h3>

            Grtz,
            --
            Rik Wasmus


            Comment

            • McHenry

              #7
              Re: Simple Regular Expression ?


              "Rik" <luiheidsgoeroe @hotmail.com> wrote in message
              news:d3c89$44a1 0ae8$8259c69c$2 5584@news1.tude lft.nl...[color=blue]
              > McHenry wrote:[color=green]
              >> "Rik" <luiheidsgoeroe @hotmail.com> wrote in message
              >> news:ab90a$449f de69$8259c69c$1 0096@news1.tude lft.nl...[color=darkred]
              >>> McHenry wrote:
              >>>> Why could we not simply have used as this is what I tried and it
              >>>> didn't work ?
              >>>> <h3>.*?(?P<fiel d2>[0-9\.,]*).*?</h3>
              >>>
              >>> 1. It matches a single dot or comma, not desired.
              >>> 2. It matches 'nothing' (* = 0 or more)
              >>>
              >>> In <h3>.*?(?P<fiel d2>[0-9]+[0-9\.,]*).*?</h3>, we use [0-9]+ to say:
              >>> once you have found at least 1 number, and maybe more, capture all
              >>> numbers, comma's and dot's.[/color]
              >>
              >> Thanks Rik, I understand the difference... wow are these things
              >> normally easy to follow or always this heard ?[/color]
              >
              > YOu'll just have to get used to it, the more you use them, the easier they
              > become. One reason I normally reply to regex questions is to sharpen my
              > skills :-).
              >[color=green]
              >> I have a regex, that performs three captures of three prices, it
              >> works when all three are present and numeric however if one is
              >> missing or listed as POA or similar then all three fail. Can this be
              >> overcome or do I need three seperate preg_match statements ?
              >>
              >> Thanks in advance...[/color]
              >
              > With listed as 'POA' you mean it literally?
              > If you're using the simple one:
              >
              > <h3>.*?(?:(?P<p rices>(?:[0-9]+[0-9\.,]*)|POA).*?){1,3 }</h3>
              >
              > Grtz,
              > --
              > Rik Wasmus
              >
              >[/color]

              Rik, thanks as always. POA was simply an example it could be anything.
              What I meant was if the one regex performs three caputres in the one
              statement and one fails must all three fail or can the other two still
              capture ?


              Comment

              • Rik

                #8
                Re: Simple Regular Expression ?

                McHenry wrote:[color=blue][color=green]
                >> With listed as 'POA' you mean it literally?
                >> If you're using the simple one:
                >>
                >> <h3>.*?(?:(?P<p rices>(?:[0-9]+[0-9\.,]*)|POA).*?){1,3 }</h3>[/color]
                > Rik, thanks as always. POA was simply an example it could be anything.
                > What I meant was if the one regex performs three caputres in the one
                > statement and one fails must all three fail or can the other two still
                > capture ?[/color]

                <h3>.*?(?:(?P<p rices>[0-9]+[0-9\.,]*).*?){0,3}</h3>

                Will search for max three prices, will allow none.

                <h3>.*?(?:(?P<p rices>[0-9]+[0-9\.,]*).*?){1,3}</h3>

                Will search for max three prices, will allow at least one.

                <h3>.*?(?:(?P<p rices>[0-9]+[0-9\.,]*).*?)+?</h3>

                Will search for as many prices as there are, will allow at least one.

                <h3>.*?(?:(?P<p rices>[0-9]+[0-9\.,]*).*?)*?</h3>

                Will search for as many prices as there are, will allow none.

                Grtz,
                --
                Rik Wasmus


                Comment

                Working...