472,121 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,121 software developers and data experts.

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 1751
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 Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.