Foo Ji-Haw wrote:

> Hi all,
> 
> I am trying to figure out how to construct a regex that will match words 
> containing a 'subword'. For example: all words containing 'ain', 
> including pain, main, sain't
> 
> My rudimentary test code:
> 
> my $text = "pain the rain in spain falls mainly on the plains.the 
> meaning of \n".
>     "ain't that cool?saint sistine chapel";
> while ($text =~ /\b(\S*?ain\S*?)\b/g)
> {
>     print $1."\n";
> }
> 
> failed for words ain't and saint
> 
> I tried to throw away \b:
> while ($text =~ 
> /[?\s,\.\?\!\(\)]([^\s,\.\?\!\(\)]*?ain[^\s,\.\?\!\(\)]*?)[?\s,\.\?\!\(\)]/g)
> 
> but that created another set of problems: I can't catch the first word 
> in the string, and the regex looks really ugly.
> 
> Can anyone write a better regex? Please advise. Thanks.

Works for me - I also tried \w vs \S with varying results and dropped the ?'s.
You could use something like [\w']* to be even more specific :

$_ = <<'EOD';
pain the rain in spain falls mainly on the plains. the meaning of
ain't that cool?saint sistine chapel
EOD

while (/\b([\w']*ain[\w']*)\b/gs) {
        print "$1\n";
}

Output:
pain
rain
spain
mainly
plains
ain't
saint
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to