In a message dated 5/18/2006 6:39:10 A.M. Eastern Standard Time, [EMAIL PROTECTED] writes:
 
> > 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
also -- the \b word boundary assertion seems unnecessary, likewise
the /s ``dot-matches-newline'' regex switch as dot is not used.  
 
(please excuse word wrap)  
 
C:[EMAIL PROTECTED]>perl -we "use strict; my $wchar = qr/[-\w']/; my $text = shift;
print qq($1 \n) while $text =~ / ( $wchar* ain $wchar* ) /gx" "pain foo ain't bar
ai saint in t'ain't taint paine bar xxxainxxxainxxx an 'ain' spain-main"
pain
ain't
saint
t'ain't
taint
paine
xxxainxxxainxxx
'ain'
spain-main
 
hth -- bill walters  
 
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to