Hello,
I have just started using a perl (and any language for that matter) a couple of days ago and am stuck on a problem. I am trying to find a word in a string and print the matched word along with 2 characters preceding and trailing the match.
For eg. If searching for the word “perl” n the string 123perl456perl789perl10 the output should be
23perl45
56perl78
89perl10
Here is the code I have written:
while ($string=~m/(.{2}perl.{2})/g) {print results "$1\n"}
The problem is that I get 23perl45 and 89perl10 but not 56perl78. I am assuming that I need to somehow change the place where the g modifier resets to after the match but I dont know how to do that.
Thanks
Thanks for the replies everyone! That solved it. (Dont know why my posts lose all formatting and paragraphs though)