469,902 Members | 1,944 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,902 developers. It's quick & easy.

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><FONTSIZE="1"><FONTFACE="Verdana,Helvetica, sans-serif">2-19-23-27-29-40</B></TD>
Any help would be appreciated.


Jul 19 '05 #1
3 1695
Jim Doughty wrote:
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);
}


\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

Jul 19 '05 #2
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:
Jim Doughty wrote:
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);
}

\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?


Jul 19 '05 #3
[ Please put your response *after* the quoted part that you include in
your reply. ]

Jim Doughty wrote:
I now understand why mine failed to match. Many Thanks.
You are welcome.
BTW, what does ?: mean at the beginning of the regex you provided.


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

http://www.perldoc.com/perl5.8.0/pod/perlre.html

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

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Bradley Plett | last post: by
4 posts views Thread by Neri | last post: by
10 posts views Thread by Lee Kuhn | last post: by
3 posts views Thread by James D. Marshall | last post: by
7 posts views Thread by Billa | last post: by
9 posts views Thread by Pete Davis | last post: by
3 posts views Thread by Zach | last post: by
25 posts views Thread by Mike | last post: by
3 posts views Thread by Mr.Steskal | last post: by
18 posts views Thread by Lit | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.