CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Perl regex help.

 



jaimitoc30
Novice

Apr 10, 2012, 10:31 AM

Post #1 of 6 (1652 views)
Perl regex help. Can't Post

Hi, I am trying to make a regular expression to detect one of the following:

acti
baja
info
xx.xx xxxxxxxx <--- where x are numbers 0-9, example:
5.00 65436765
10.00 65436765

I was able to match the last one using this regex:


Code
$text =~ /^\s*([0-9]{1,2}[]{0,1}|[0-5]{1,2}[][0,5]{0,1}[0]{0,1})\s+([0-9]{8})\s*$/;


And now, I want to include the first 3 strings in one regex:


Code
text =~ /^\s*([0-9]{1,2}[]{0,1}|[0-5]{1,2}[][0,5]{0,1}[0]{0,1})\s+([0-9]{8})|(acti|baja|info)\s*$/;


but it won't work for none of the examples it should match above.

Can somebody give me some lights here?

Thank you and regards.


BillKSmith
Veteran

Apr 10, 2012, 1:00 PM

Post #2 of 6 (1645 views)
Re: [jaimitoc30] Perl regex help. [In reply to] Can't Post

I must not understand your problem. I have prepared a complete perl program to demonstrate my understanding of it.


Code
use strict; 
use warnings;
my @samples = (
'acti',
'baja',
'info',
'5.00 65436765',
'10.00 65436765',
);
my $regex1 =
qr/^\s*([0-9]{1,2}[]{0,1}|[0-5]{1,2}[][0,5]{0,1}[0]{0,1})\s+([0-9]{8})\s*$/;
my $regex2 =
qr/^\s*([0-9]{1,2}[]{0,1}|[0-5]{1,2}[][0,5]{0,1}[0]{0,1})\s+([0-9]{8})|
(acti|baja|info)\s*$
/x;

foreach my $text (@samples) {
if ($text =~ $regex1) {
print "Regex_1 Matched: $text\n";
}
if ($text =~ $regex2) {
print "Regex_2 Matched: $text\n";
}
}



You expect $regex1 to match the numeric examples and $regex2 to match all the examples. You claim that $regex1 matches something, but $regex2 does not. This is not at all the case. $regex2 matches the text examples. $regex1 does not match anything.



Please post a complete program that demonstrates your problem. Explain what is wrong with its output.
Good Luck,
Bill


jaimitoc30
Novice

Apr 10, 2012, 1:09 PM

Post #3 of 6 (1643 views)
Re: [BillKSmith] Perl regex help. [In reply to] Can't Post


In Reply To
I must not understand your problem. I have prepared a complete perl program to demonstrate my understanding of it.


Code
use strict; 
use warnings;
my @samples = (
'acti',
'baja',
'info',
'5.00 65436765',
'10.00 65436765',
);
my $regex1 =
qr/^\s*([0-9]{1,2}[]{0,1}|[0-5]{1,2}[][0,5]{0,1}[0]{0,1})\s+([0-9]{8})\s*$/;
my $regex2 =
qr/^\s*([0-9]{1,2}[]{0,1}|[0-5]{1,2}[][0,5]{0,1}[0]{0,1})\s+([0-9]{8})|
(acti|baja|info)\s*$
/x;

foreach my $text (@samples) {
if ($text =~ $regex1) {
print "Regex_1 Matched: $text\n";
}
if ($text =~ $regex2) {
print "Regex_2 Matched: $text\n";
}
}



You expect $regex1 to match the numeric examples and $regex2 to match all the examples. You claim that $regex1 matches something, but $regex2 does not. This is not at all the case. $regex2 matches the text examples. $regex1 does not match anything.



Please post a complete program that demonstrates your problem. Explain what is wrong with its output.



Ok, I have tested it:

localhost# perl regexval2.pl
Regex_2 Matched: acti
Regex_2 Matched: baja
Regex_2 Matched: info
Regex_1 Matched: 5.00 65436765
Regex_2 Matched: 5.00 65436765
Regex_1 Matched: 10.00 65436765
Regex_2 Matched: 10.00 65436765

Now, look at this code:



Code
#!/usr/local/bin/perl 

$text="acti";
$text =~ qr/^\s*([0-9]{1,2}[]{0,1}|[0-5]{1,2}[][0,5]{0,1}[0]{0,1})\s+([0-9]{8})|
(acti|baja|info)\s*$
/x;
$text= $1;
print "RESULT: $text\n";
print "END";


The result is this:

localhost# perl regexval.pl
RESULT:
END

it won't match, I did a copy paste.. whats the difference then??


BillKSmith
Veteran

Apr 10, 2012, 3:33 PM

Post #4 of 6 (1635 views)
Re: [jaimitoc30] Perl regex help. [In reply to] Can't Post

The answer to your last question is easy. It does match. The result you want is in $3, not $1. You can fix this problem by replacing all your parenthesis with non-capturing parenthesis and add one set of parenthesis around the entire regex to capture the final result.

I do not get the same result from regexval2.pl as you do. You are probably using a newer version of perl. (I am using ActiveState perl v5.8.8 - Sorry if I misled you in my original post)
Good Luck,
Bill


jaimitoc30
Novice

Apr 11, 2012, 7:39 AM

Post #5 of 6 (1590 views)
Re: [BillKSmith] Perl regex help. [In reply to] Can't Post

Don't worry at all. Thank you very much for your assistance, I was able to resolve this by using this code:


Code
$text="5.00 66851169"; 
my $regex2 = qr/^\s*([0-9]{1,2}[]{0,1}|[0-5]{1,2}[][0,5]{0,1}[0]{0,1})\s+([0-9]{8})| (acti|baja|info)\s*$ /x;
$text =~ $regex2;
print "RESULT: $text\n";
print "END";



Thank you very much for your time and assistance.

Regards,
Jaime


rovf
Veteran

Apr 12, 2012, 4:13 AM

Post #6 of 6 (1477 views)
Re: [jaimitoc30] Perl regex help. [In reply to] Can't Post

I don't think that this regexp really (i.e. in *every* case does exactly what you want it to do. Note that this regexp contains the following sequence of characters:


Code
 .... []{0,1} ....


The closing bracket symbol ']' in this case does NOT close the opening bracket to its left. This is a special rule that a closing bracket immediately following an open bracket, means a literal closing bracket.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives