Regular Expressions: Grouping and backreferences...

2002-09-10 Thread eric-perl

Hello, All:

How can I capture all the words that contain 'at' in the string 'A fat cat
sat on my hat.'?

Any pointers?

$sentence = 'A fat cat sat on my hat.'
$sentence =~ m/(\wat)/;

returns:

$1 = 'fat'

-- 
Eric P.
Sunnyvale, CA


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Regular Expressions: Grouping and backreferences...

2002-09-10 Thread Mark Anderson

-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, September 10, 2002 12:32 AM
To: Beginners Perl Mailing List
Subject: Regular Expressions: Grouping and backreferences...


Hello, All:

How can I capture all the words that contain 'at' in the string 'A fat cat
sat on my hat.'?

Any pointers?

$sentence = 'A fat cat sat on my hat.'
$sentence =~ m/(\wat)/;

.returns:

$1 = 'fat'

-Response Message-

your regex will only match the letter before at and the 'at', all words
containing 'at' is the following, placing them into the array called @list:

$sentence = 'A fat cat sat on that hat.';
@list = $sentence =~ m/(\w*at\w*)/g;

foreach (@list) {print "$_\n"}


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Regular Expressions: Grouping and backreferences...

2002-09-10 Thread Chas Owens

On Tue, 2002-09-10 at 03:32, [EMAIL PROTECTED] wrote:
> Hello, All:
> 
> How can I capture all the words that contain 'at' in the string 'A fat cat
> sat on my hat.'?
> 
> Any pointers?
> 
> $sentence = 'A fat cat sat on my hat.'
> $sentence =~ m/(\wat)/;
> 
> returns:
> 
> $1 = 'fat'
> 
> -- 
> Eric P.
> Sunnyvale, CA
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
>

You were on the right track, but you need to do global matching and
define "contains" better.  Here is what I would do.


The "/g" modifier specifies global pattern match­
ing--that is, matching as many times as possible
within the string.  How it behaves depends on the
context.  In list context, it returns a list of
the substrings matched by any capturing parenthe­
ses in the regular expression.  If there are no
parentheses, it returns a list of all the matched
strings, as if there were parentheses around the
whole pattern.



#!/usr/bin/perl

my $str = 'A fat cat sat on my hat and attacked me.';
my @at_words = $str =~ /(\w*at\w*\b)/g;
print "@at_words\n";



fat cat sat hat attacked


 
-- 
Today is Pungenday the 34th day of Bureaucracy in the YOLD 3168
Or not.

Missile Address: 33:48:3.521N  84:23:34.786W


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Regular Expressions: Grouping and backreferences...

2002-09-10 Thread Janek Schleicher

eric-perl wrote at Tue, 10 Sep 2002 09:32:22 +0200:

> How can I capture all the words that contain 'at' in the string 'A fat cat
> sat on my hat.'?
> 
> Any pointers?
> 
> $sentence = 'A fat cat sat on my hat.'
> $sentence =~ m/(\wat)/;
> 
> returns:
> 
> $1 = 'fat'

As TMTWTDI, here's a solution without a global matching:

my @at_words = grep /at/, split /\W+/, $sentence;


Greetings,
Janek


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]