Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.perl.misc

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 01-06-2010, 09:47 AM
Justin C
Guest
 
Posts: n/a
Default Regex, spaces in pattern stored in variable.

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.
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 01-06-2010, 10:01 AM
Peter Makholm
Guest
 
Posts: n/a
Default Re: Regex, spaces in pattern stored in variable.

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.

Reply With Quote
  #3 (permalink)  
Old 01-06-2010, 10:56 AM
Justin C
Guest
 
Posts: n/a
Default Re: Regex, spaces in pattern stored in variable.

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.
Reply With Quote
  #4 (permalink)  
Old 01-06-2010, 11:09 AM
Ben Morrow
Guest
 
Posts: n/a
Default Re: Regex, spaces in pattern stored in variable.


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

Reply With Quote
  #5 (permalink)  
Old 01-06-2010, 01:46 PM
Justin C
Guest
 
Posts: n/a
Default Re: Regex, spaces in pattern stored in variable.

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.
Reply With Quote
 
Reply

Popular Tags in the Forum
pattern, regex, spaces, stored, variable

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


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



All times are GMT. The time now is 12:15 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.