in reply to Contents of $2 empty
So, using your data (and adding the "... saw Wilma yesterday" part of the exercise statement):
while(<DATA>) { chomp; if ( /(Wilma)([\w\W]{5})/i ) { print "Matched: |<$1> <$2>\n"; } else { print "No match: |$_|\n"; } } __DATA__ This is a line that has the name fred in it. This is a line that does not. This is a line that has the name barney in it. This is fred. This is notfred. Hello Fred Flintstone! Hello Fred! Allo allo, Alfred! Cheerio Mr. frederick! Hello Mr. Slate! Hello FRED! Hello Fred! Hello fred! This is some wilma text with some words in it this is fred. This is wilmafred. This is some more wilma text fred. Mrs. Wilma Flintstone wilma&fred wilma but not barney I saw Wilma yesterday.
Output:
perl 732070.pl No match: |This is a line that has the name fred in it.| No match: |This is a line that does not.| No match: |This is a line that has the name barney in it.| No match: |This is fred.| No match: |This is notfred.| No match: |Hello Fred Flintstone!| No match: |Hello Fred!| No match: |Allo allo, Alfred!| No match: |Cheerio Mr. frederick!| No match: |Hello Mr. Slate!| No match: |Hello FRED!| No match: |Hello Fred!| No match: |Hello fred!| Matched: |<wilma> < text> Matched: |<wilma> <fred.> Matched: |<wilma> < text> Matched: |<Wilma> < Flin> Matched: |<wilma> <&fred> Matched: |<wilma> < but > Matched: |<Wilma> < yest>
Caveat: Neither my Llama nor my Camel edition has the exercise you describe, so this may miss a requirement that [Ww]ilma be followed by a space or by a punctuation mark or symbol, which would substantially change the regex required.
Update: ...and, oh yes, I've simplified your print "Matched.... as well, again in the pursuit of KISS.
|
|---|