Help needed with a Regular Expression

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

    #1

    Help needed with a Regular Expression

    I'm having problems coming up with a regular expression that works for what
    I want. I need to extract a few numbers from from html. Here's a snippet:

    <tr><td align="right">C onsolidated Metro Area (CMSA)</td><td
    align="left"><s trong> SEATTLE-TACOMA-BREMERTON, WA
    ( 7602 )</td></tr>
    <tr><td align="right">P rimary Metro Area (PMSA)</td><td
    align="left"><s trong>SEATTLE-BELLEVUE-EVERETT, WA
    ( 7600 )</td></tr>

    What I am trying to do is extract the first 4 digit number that occurs after
    the letters (CMSA) and the first 4 digit number that occurs after (PMSA).

    In Dreamweaver, I am able to use the following reg ex to search and it works
    fine:

    \(CMSA\).*\d{4}

    When I try to use the same reg ex in the code on a page it fails to make a
    match:

    $getCMSA = eregi("\(CMSA\) .*\(\s*\d{4}",$ result,$CMSA_va lue);

    I want the reg ex to be as flexible as can be with the code (the least
    amount of literal character searches) it is searching so if the site the
    values are being pulled from ever changes their code slightly, there will be
    a higher chance of the reg ex still working.

    Can anybody help me out here? It would be GREATLY appreciated. Thanks!


  • Doug Hutcheson

    #2
    Re: Help needed with a Regular Expression

    A regexp along these lines does what you want - conversion to eregi or other
    syntax as required I leave as an exercise for the reader:
    (?:[C|P]MSA.*?)(\d{4})
    Cheers,
    Doug

    --
    Remove the blots from my address to reply
    "MJ" <no_spam@thank. you> wrote in message
    news:JYxvc.157$ Q03.171859@news .uswest.net...[color=blue]
    > I'm having problems coming up with a regular expression that works for[/color]
    what[color=blue]
    > I want. I need to extract a few numbers from from html. Here's a[/color]
    snippet:[color=blue]
    >
    > <tr><td align="right">C onsolidated Metro Area (CMSA)</td><td
    > align="left"><s trong> SEATTLE-TACOMA-BREMERTON, WA
    > ( 7602 )</td></tr>
    > <tr><td align="right">P rimary Metro Area (PMSA)</td><td
    > align="left"><s trong>SEATTLE-BELLEVUE-EVERETT, WA
    > ( 7600 )</td></tr>
    >
    > What I am trying to do is extract the first 4 digit number that occurs[/color]
    after[color=blue]
    > the letters (CMSA) and the first 4 digit number that occurs after (PMSA).
    >
    > In Dreamweaver, I am able to use the following reg ex to search and it[/color]
    works[color=blue]
    > fine:
    >
    > \(CMSA\).*\d{4}
    >
    > When I try to use the same reg ex in the code on a page it fails to make a
    > match:
    >
    > $getCMSA = eregi("\(CMSA\) .*\(\s*\d{4}",$ result,$CMSA_va lue);
    >
    > I want the reg ex to be as flexible as can be with the code (the least
    > amount of literal character searches) it is searching so if the site the
    > values are being pulled from ever changes their code slightly, there will[/color]
    be[color=blue]
    > a higher chance of the reg ex still working.
    >
    > Can anybody help me out here? It would be GREATLY appreciated. Thanks!
    >
    >[/color]


    Comment

    • Doug Hutcheson

      #3
      Re: Help needed with a Regular Expression

      > "MJ" <no_spam@thank. you> wrote in message[color=blue]
      > news:JYxvc.157$ Q03.171859@news .uswest.net...[color=green]
      > > I'm having problems coming up with a regular expression that works for[/color]
      > what[color=green]
      > > I want. I need to extract a few numbers from from html. Here's a[/color]
      > snippet:[color=green]
      > >
      > > <tr><td align="right">C onsolidated Metro Area (CMSA)</td><td
      > > align="left"><s trong> SEATTLE-TACOMA-BREMERTON, WA
      > > ( 7602 )</td></tr>
      > > <tr><td align="right">P rimary Metro Area (PMSA)</td><td
      > > align="left"><s trong>SEATTLE-BELLEVUE-EVERETT, WA
      > > ( 7600 )</td></tr>
      > >
      > > What I am trying to do is extract the first 4 digit number that occurs[/color]
      > after[color=green]
      > > the letters (CMSA) and the first 4 digit number that occurs after[/color][/color]
      (PMSA).[color=blue][color=green]
      > >
      > > In Dreamweaver, I am able to use the following reg ex to search and it[/color]
      > works[color=green]
      > > fine:
      > >
      > > \(CMSA\).*\d{4}
      > >
      > > When I try to use the same reg ex in the code on a page it fails to make[/color][/color]
      a[color=blue][color=green]
      > > match:
      > >
      > > $getCMSA = eregi("\(CMSA\) .*\(\s*\d{4}",$ result,$CMSA_va lue);
      > >
      > > I want the reg ex to be as flexible as can be with the code (the least
      > > amount of literal character searches) it is searching so if the site the
      > > values are being pulled from ever changes their code slightly, there[/color][/color]
      will[color=blue]
      > be[color=green]
      > > a higher chance of the reg ex still working.
      > >
      > > Can anybody help me out here? It would be GREATLY appreciated. Thanks!
      > >
      > >[/color]
      >
      >[/color]
      "Doug Hutcheson" <doug.blot.hutc heson@nrm.blot. qld.blot.gov.bl ot.au> wrote
      in message news:5Jyvc.21$E P1.1485@news.op tus.net.au...[color=blue]
      > A regexp along these lines does what you want - conversion to eregi or[/color]
      other[color=blue]
      > syntax as required I leave as an exercise for the reader:
      > (?:[C|P]MSA.*?)(\d{4})
      > Cheers,
      > Doug
      >
      > --
      > Remove the blots from my address to reply[/color]


      Damn!
      Using brain-dead M$ tools for posting and I forgot to cut 'n paste top post
      to bottom post. Hereby fixed!
      Apologies, all
      "8-\

      --
      Remove the blots from my address to reply


      Comment

      • John Dunlop

        #4
        Re: Help needed with a Regular Expression

        Doug Hutcheson wrote:
        [color=blue]
        > [MJ wrote:]
        >[color=green]
        > > What I am trying to do is extract the first 4 digit number that occurs
        > > after the letters (CMSA) and the first 4 digit number that occurs after
        > > (PMSA).[/color][/color]

        [ ... ]
        [color=blue]
        > A regexp along these lines does what you want - conversion to eregi or other
        > syntax as required I leave as an exercise for the reader:
        > (?:[C|P]MSA.*?)(\d{4})[/color]

        Yes, you'd need a non-greedy match. But I reckon the first problem
        was the dot metacharacter not matching newlines. By default, dot
        metacharacters don't match newlines in PCREs *or* POSIX regular
        expressions. If the numbers and letters weren't on the same line,
        there could be no match.

        Also, to match the first four-digit number, the characters after a
        four-digit sequence must be checked to make sure they aren't digits.

        So, assuming parentheses surround the letters, here's one PCRE
        possibility (untested):

        `\([CP]MSA\).*?\d{4}(? !\d)`s

        --
        Jock

        Comment

        • OhBoy!

          #5
          Re: Help needed with a Regular Expression

          That did the trick. Thank you VERY much!


          "Doug Hutcheson" <doug.blot.hutc heson@nrm.blot. qld.blot.gov.bl ot.au> wrote
          in message news:5Jyvc.21$E P1.1485@news.op tus.net.au...[color=blue]
          > A regexp along these lines does what you want - conversion to eregi or[/color]
          other[color=blue]
          > syntax as required I leave as an exercise for the reader:
          > (?:[C|P]MSA.*?)(\d{4})
          > Cheers,
          > Doug
          >[/color]


          Comment

          • Doug Hutcheson

            #6
            Re: Help needed with a Regular Expression

            "John Dunlop" <usenet+2004@jo hn.dunlop.name> wrote in message
            news:MPG.1b2968 466f917f4f98973 4@News.Individu al.NET...[color=blue]
            > Doug Hutcheson wrote:
            >[color=green]
            > > [MJ wrote:]
            > >[color=darkred]
            > > > What I am trying to do is extract the first 4 digit number that occurs
            > > > after the letters (CMSA) and the first 4 digit number that occurs[/color][/color][/color]
            after[color=blue][color=green][color=darkred]
            > > > (PMSA).[/color][/color]
            >
            > [ ... ]
            >[color=green]
            > > A regexp along these lines does what you want - conversion to eregi or[/color][/color]
            other[color=blue][color=green]
            > > syntax as required I leave as an exercise for the reader:
            > > (?:[C|P]MSA.*?)(\d{4})[/color]
            >
            > Yes, you'd need a non-greedy match. But I reckon the first problem
            > was the dot metacharacter not matching newlines. By default, dot
            > metacharacters don't match newlines in PCREs *or* POSIX regular
            > expressions. If the numbers and letters weren't on the same line,
            > there could be no match.
            >
            > Also, to match the first four-digit number, the characters after a
            > four-digit sequence must be checked to make sure they aren't digits.
            >
            > So, assuming parentheses surround the letters, here's one PCRE
            > possibility (untested):
            >
            > `\([CP]MSA\).*?\d{4}(? !\d)`s
            >
            > --
            > Jock[/color]

            Jock,
            Two good observations - thanks.
            This might solve those issues:
            (?:[C|P]MSA)[\S|\s]*?(\d{4}\D)
            Cheers,
            Doug

            --
            Remove the blots from my address to reply


            Comment

            • Doug Hutcheson

              #7
              Re: Help needed with a Regular Expression

              Dang it!
              That picks up the following non-digit - I'm an idjit!
              Try this:
              (?:[C|P]MSA)[\S|\s]*?(\d{4})[\D]*
              Sigh...oldtimer s disease...
              "8-\
              Doug

              --
              Remove the blots from my address to reply
              "Doug Hutcheson" <doug.blot.hutc heson@nrm.blot. qld.blot.gov.bl ot.au> wrote
              in message news:H1Ovc.34$E P1.2344@news.op tus.net.au...[color=blue]
              > "John Dunlop" <usenet+2004@jo hn.dunlop.name> wrote in message
              > news:MPG.1b2968 466f917f4f98973 4@News.Individu al.NET...[color=green]
              > > Doug Hutcheson wrote:
              > >[color=darkred]
              > > > [MJ wrote:]
              > > >
              > > > > What I am trying to do is extract the first 4 digit number that[/color][/color][/color]
              occurs[color=blue][color=green][color=darkred]
              > > > > after the letters (CMSA) and the first 4 digit number that occurs[/color][/color]
              > after[color=green][color=darkred]
              > > > > (PMSA).[/color]
              > >
              > > [ ... ]
              > >[color=darkred]
              > > > A regexp along these lines does what you want - conversion to eregi or[/color][/color]
              > other[color=green][color=darkred]
              > > > syntax as required I leave as an exercise for the reader:
              > > > (?:[C|P]MSA.*?)(\d{4})[/color]
              > >
              > > Yes, you'd need a non-greedy match. But I reckon the first problem
              > > was the dot metacharacter not matching newlines. By default, dot
              > > metacharacters don't match newlines in PCREs *or* POSIX regular
              > > expressions. If the numbers and letters weren't on the same line,
              > > there could be no match.
              > >
              > > Also, to match the first four-digit number, the characters after a
              > > four-digit sequence must be checked to make sure they aren't digits.
              > >
              > > So, assuming parentheses surround the letters, here's one PCRE
              > > possibility (untested):
              > >
              > > `\([CP]MSA\).*?\d{4}(? !\d)`s
              > >
              > > --
              > > Jock[/color]
              >
              > Jock,
              > Two good observations - thanks.
              > This might solve those issues:
              > (?:[C|P]MSA)[\S|\s]*?(\d{4}\D)
              > Cheers,
              > Doug
              >
              > --
              > Remove the blots from my address to reply
              >
              >[/color]


              Comment

              • Doug Hutcheson

                #8
                Re: Help needed with a Regular Expression

                "Doug Hutcheson" <doug.blot.hutc heson@nrm.blot. qld.blot.gov.bl ot.au> wrote
                in message news:0dOvc.36$E P1.967@news.opt us.net.au...[color=blue]
                > "Doug Hutcheson" <doug.blot.hutc heson@nrm.blot. qld.blot.gov.bl ot.au> wrote
                > in message news:H1Ovc.34$E P1.2344@news.op tus.net.au...[color=green]
                > > "John Dunlop" <usenet+2004@jo hn.dunlop.name> wrote in message
                > > news:MPG.1b2968 466f917f4f98973 4@News.Individu al.NET...[color=darkred]
                > > > Doug Hutcheson wrote:
                > > >
                > > > > [MJ wrote:]
                > > > >
                > > > > > What I am trying to do is extract the first 4 digit number that[/color][/color]
                > occurs[color=green][color=darkred]
                > > > > > after the letters (CMSA) and the first 4 digit number that occurs[/color]
                > > after[color=darkred]
                > > > > > (PMSA).
                > > >
                > > > [ ... ]
                > > >
                > > > > A regexp along these lines does what you want - conversion to eregi[/color][/color][/color]
                or[color=blue][color=green]
                > > other[color=darkred]
                > > > > syntax as required I leave as an exercise for the reader:
                > > > > (?:[C|P]MSA.*?)(\d{4})
                > > >
                > > > Yes, you'd need a non-greedy match. But I reckon the first problem
                > > > was the dot metacharacter not matching newlines. By default, dot
                > > > metacharacters don't match newlines in PCREs *or* POSIX regular
                > > > expressions. If the numbers and letters weren't on the same line,
                > > > there could be no match.
                > > >
                > > > Also, to match the first four-digit number, the characters after a
                > > > four-digit sequence must be checked to make sure they aren't digits.
                > > >
                > > > So, assuming parentheses surround the letters, here's one PCRE
                > > > possibility (untested):
                > > >
                > > > `\([CP]MSA\).*?\d{4}(? !\d)`s
                > > >
                > > > --
                > > > Jock[/color]
                > >
                > > Jock,
                > > Two good observations - thanks.
                > > This might solve those issues:
                > > (?:[C|P]MSA)[\S|\s]*?(\d{4}\D)
                > > Cheers,
                > > Doug
                > >
                > > --
                > > Remove the blots from my address to reply
                > >
                > >[/color]
                >
                > Dang it!
                > That picks up the following non-digit - I'm an idjit!
                > Try this:
                > (?:[C|P]MSA)[\S|\s]*?(\d{4})[\D]*
                > Sigh...oldtimer s disease...
                > "8-\
                > Doug
                >
                > --
                > Remove the blots from my address to reply
                >[/color]

                Double dang it!
                M$ Outlook Express does not allow me to bottom post by default.
                Forgot to copy and paste from top to bottom in my last message - sorry,
                folks.
                Fixed here.
                <beats head against keyboard: "Why won't they let me use a decent
                newsreader?" />
                Doug

                --
                Remove the blots from my address to reply


                Comment

                Working...