Regular Expression Help

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

    Regular Expression Help

    When reading lines of html text from a file, I'm trying to match the
    following, I'm trying to match on a string of 6 numbers, delimited by
    hyphens. e.g. 2-19-23-27-29-40

    The following code fails to match, and so do may different variants.

    if($inline !~ /((\d?\-){5}(\d?))/ ){
    exit(-1);
    }

    The actual value of $inline is as follows:
    <TD><B><FONTSIZ E="1"><FONTFACE ="Verdana,Helve tica,sans-serif">2-19-23-27-29-40</B></TD>


    Any help would be appreciated.




  • Gunnar Hjalmarsson

    #2
    Re: Regular Expression Help

    Jim Doughty wrote:[color=blue]
    > When reading lines of html text from a file, I'm trying to match
    > the following, I'm trying to match on a string of 6 numbers,
    > delimited by hyphens. e.g. 2-19-23-27-29-40
    >
    > The following code fails to match, and so do may different
    > variants.
    >
    > if($inline !~ /((\d?\-){5}(\d?))/ ){
    > exit(-1);
    > }[/color]

    \d? means 0 or 1 digit, while the string includes groups of more than
    one digit. And why all those parentheses? Try this:

    if ( $inline !~ /(?:\d+-){5}\d+/ ) {

    Btw, you said that you are reading *lines* of text. In that case, the
    script will exit unless every line matches. Is that what you want?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl

    Comment

    • Jim Doughty

      #3
      Re: Regular Expression Help

      Gunnar:

      I now understand why mine failed to match. Many Thanks. I use that
      string to validate the format of the data, only agains one line. I want
      ot exit becase if the data is bad there is no point in continuing.

      BTW, what does ?: mean at the beginning of the regex you provided.

      Again, Many Thanks!

      Jim



      Gunnar Hjalmarsson wrote:[color=blue]
      > Jim Doughty wrote:
      >[color=green]
      >> When reading lines of html text from a file, I'm trying to match
      >> the following, I'm trying to match on a string of 6 numbers,
      >> delimited by hyphens. e.g. 2-19-23-27-29-40
      >>
      >> The following code fails to match, and so do may different
      >> variants.
      >>
      >> if($inline !~ /((\d?\-){5}(\d?))/ ){
      >> exit(-1);
      >> }[/color]
      >
      >
      > \d? means 0 or 1 digit, while the string includes groups of more than
      > one digit. And why all those parentheses? Try this:
      >
      > if ( $inline !~ /(?:\d+-){5}\d+/ ) {
      >
      > Btw, you said that you are reading *lines* of text. In that case, the
      > script will exit unless every line matches. Is that what you want?
      >[/color]

      Comment

      • Gunnar Hjalmarsson

        #4
        Re: Regular Expression Help

        [ Please put your response *after* the quoted part that you include in
        your reply. ]

        Jim Doughty wrote:[color=blue]
        > I now understand why mine failed to match. Many Thanks.[/color]

        You are welcome.
        [color=blue]
        > BTW, what does ?: mean at the beginning of the regex you provided.[/color]

        Finding that out is a good reason to acquaint yourself with the
        documentation of regular expressions in Perl. ;-)



        --
        Gunnar Hjalmarsson
        Email: http://www.gunnar.cc/cgi-bin/contact.pl

        Comment

        Working...