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:
regex

 



tazengr
New User

Jul 27, 2010, 7:54 PM

Post #1 of 5 (404 views)
regex Can't Post

Hello,
I have a regular expression where I have a string where I would like to match both hyphenated and spaced strings. The string has large gaps (more than one whitespace character) between the matches that I would like to return. Anyway here is a sample line of the file.

Pat Starks Dodge Tx 78666-2301

I can write two separate regex to return the one spaced strings and hyphenated strings without a problem. However, when I combine them using an or I don't get what is expected.

while($_ =~ m/(\b\w+\-*\w*\b)/g){
print "the match is $1\n";

The above returns the hyphenated strings but not spaced ones.

while($_ =~ m/(\b\w+\s?\w*\b)/g){
print "the match is $1\n";

This one returns spaced but not hyphenated strings.

while($_ =~ m/(\b\w+(\s?|-*)\w*\b)/g){
print "the match is $1\n";

I tried the or above but it only matches the spaced ones as well.

Any suggestions?
Thanks,
TazEngr


BillKSmith
Veteran

Jul 27, 2010, 8:46 PM

Post #2 of 5 (401 views)
Re: [tazengr] regex [In reply to] Can't Post


Code
use strict; 
use warnings;

my $line = 'Pat Starks Dodge Tx 78666-2301';

my @strings = $line =~ m/(\b\w+[\s-]\w*\b)/g;
print join "\n", @strings;





result:


Code
Pat Starks 
Dodge Tx
78666-2301

Good Luck,
Bill


tazengr
New User

Jul 27, 2010, 9:30 PM

Post #3 of 5 (400 views)
Re: [BillKSmith] regex [In reply to] Can't Post

Thanks for the quick response.
I tried that on my file and am still not able to match both types of strings.
Here is the code:

use strict;
use warnings;

open(INPUT_FILE, '<', "log.txt");
my @raw_data = <INPUT_FILE>;
close(INPUT_FILE);
foreach(@raw_data){
while($_ =~ m/(\b\w+[\s-]\w*\b)/g){
print "the match is $1\n";
}
}
exit(0);

from the command line I just run the perl .pl file.
Attachments: log.txt (0.22 KB)


BillKSmith
Veteran

Jul 28, 2010, 4:18 AM

Post #4 of 5 (388 views)
Re: [tazengr] regex [In reply to] Can't Post

Your new code works fine on the data you provided. Show me the data where it fails, and the result that you expect for that data.
Good Luck,
Bill


vishwa787
New User

Jul 29, 2010, 7:04 AM

Post #5 of 5 (373 views)
Re: [BillKSmith] regex [In reply to] Can't Post

Try this:

$line =~ /(.*\s+)*(.*-.*)*/g

print "$1 and $2 \n";

 
 


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

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