|
|||
|
I have a list of strings, some contain spaces. The strings are used as
patterns for a regex match, they don't seem to be working! I tried substituting the space with '\s' but I got warnings, and still no match. I'm sure there's a way to do this, but Google isn't providing any answers (more likely I don't know how to formulate a useful search). Anyway, here's what I have: #!/usr/bin/perl use warnings; use strict; my @list = ( "Fred Flintstone", "Barney Rubble", ); while (<DATA>) { my $string = chomp $_; foreach (@list) { if ( $string =~ /($_)/ ) { print "Matched ", $1, "\n"; } } } __DATA__ A man called Fred Flintstone lives in a cave. Fred's neighbour is called Barney Rubble. Fred is married to Wilma Flintstone and Barney Rubble is married to Betty. Justin. -- Justin C, by the sea. |
|
|
||||
|
||||
|
|
|
|||
|
Justin C <justin.0911@purestblue.com> writes:
> #!/usr/bin/perl > > use warnings; > use strict; > > my @list = ( > "Fred Flintstone", > "Barney Rubble", > ); > > while (<DATA>) { > my $string = chomp $_; 'perldoc -f chomp' says: [...] It returns the total number of characters removed from all its arguments. [...] So $string would have the value 1. Using the perl debugger it would have been very easy to check that you assumptions about the content of $string and $_ was true at the conditional. > foreach (@list) { > if ( $string =~ /($_)/ ) { > print "Matched ", $1, "\n"; > } > } > } > > __DATA__ > A man called Fred Flintstone lives in a cave. > Fred's neighbour is called Barney Rubble. > Fred is married to Wilma Flintstone and Barney Rubble is married to Betty. > > Justin. |
|
|||
|
On 2010-01-06, Peter Makholm <peter@makholm.net> wrote:
> Justin C <justin.0911@purestblue.com> writes: > >> #!/usr/bin/perl >> >> use warnings; >> use strict; >> >> my @list = ( >> "Fred Flintstone", >> "Barney Rubble", >> ); >> >> while (<DATA>) { >> my $string = chomp $_; > > 'perldoc -f chomp' says: > > [...] It returns the total number of characters removed from all its > arguments. [...] Doh! Thank you. Justin. -- Justin C, by the sea. |
|
|||
|
Quoth Justin C <justin.0911@purestblue.com>: > I have a list of strings, some contain spaces. The strings are used as > patterns for a regex match, they don't seem to be working! I tried > substituting the space with '\s' but I got warnings, and still no match. > > I'm sure there's a way to do this, but Google isn't providing any > answers (more likely I don't know how to formulate a useful search). > > Anyway, here's what I have: > > #!/usr/bin/perl > > use warnings; > use strict; > > my @list = ( > "Fred Flintstone", > "Barney Rubble", > ); > > while (<DATA>) { > my $string = chomp $_; chomp; > foreach (@list) { It's very confusing to have several nested loops all looping with $_. for my $match (@list) { > if ( $string =~ /($_)/ ) { You normally want \Q\E when interpolating a plain string into a regex: if (/(\Q$match\E)/) { Ben |
|
|||
|
On 2010-01-06, Ben Morrow <ben@morrow.me.uk> wrote:
> > Quoth Justin C <justin.0911@purestblue.com>: >> I have a list of strings, some contain spaces. The strings are used as >> patterns for a regex match, they don't seem to be working! I tried >> substituting the space with '\s' but I got warnings, and still no match. >> >> I'm sure there's a way to do this, but Google isn't providing any >> answers (more likely I don't know how to formulate a useful search). >> >> Anyway, here's what I have: >> >> #!/usr/bin/perl >> >> use warnings; >> use strict; >> >> my @list = ( >> "Fred Flintstone", >> "Barney Rubble", >> ); >> >> while (<DATA>) { >> my $string = chomp $_; > > chomp; > >> foreach (@list) { > > It's very confusing to have several nested loops all looping with $_. > You're telling me! > for my $match (@list) { > >> if ( $string =~ /($_)/ ) { > > You normally want \Q\E when interpolating a plain string into a regex: > > if (/(\Q$match\E)/) { Ah, yes, I see. That makes sense (this is, I think, my first pattern in a scalar). > > Ben > Justin. -- Justin C, by the sea. |
|
|
![]() |
| Popular Tags in the Forum |
| pattern, regex, spaces, stored, variable |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Re: Macro question | Joe Matise | Newsgroup comp.soft-sys.sas | 0 | 06-01-2009 06:25 PM |
| FAQ 6.9 How can I quote a variable to use in a regex? | PerlFAQ Server | Newsgroup comp.lang.perl.misc | 0 | 05-01-2009 01:03 AM |
| Treedisc Macro | Alex Murphy | Newsgroup comp.soft-sys.sas | 0 | 04-28-2009 07:04 AM |
| convert variable name to observation stored in an array | dc353@hotmail.com | Newsgroup comp.soft-sys.sas | 2 | 11-03-2008 12:29 PM |
| Re: macro variable contains invalid chars | Fehd, Ronald J. | Newsgroup comp.soft-sys.sas | 1 | 10-25-2004 01:38 PM |