What constitutes the end of a "sentence"? How will your script get the "certain type of words (patterns)" that it needs to extract?
If the output needs to refer to sentence number, you must first split each line into an array of sentences. So that's where you would use a regex to match the sentence boundary (whatever that may be).
Then, looping over the sentences on a line, you check for matches to your target word (whatever that may be). If the search target is a regex (e.g. /f(?:oo|o?u)/ to match any of "foo", "fu" or "fou"), identifying the position of the match as a character offset within the sentence could be done as a two step process: get the match, then find its offset:
while (<>) { # read a string from input
my @senteces = split /\.\s+/; # ". " might work for splitting int
+o sentences(?)
for my $i ( 0 .. $#sentences ) {
if ( $sentence[$i] =~ /(?<!\S)(f(?:oo|o?u))(?!\S)/ )
my $match = $1;
my $position = index( $sentence[$i], $match );
printf( "%s found in line %d, sentence %d\n",
$match, $., $i+1 );
}
}
}
(not tested)
The big ugly regex is using negative look-behind and negative look-ahead (see perlre) in order to make sure that "foo"/"fou"/"fu" is matched only when not part of a larger word (e.g. "food", "afoul" and "snafu" will not produce matches, because the target is preceded and/or followed by a non-whitespace character).
There's a good chance that this snippet won't do exactly what you want, but if you don't show us any code you've tried, or any sample input with desired output, you can't expect much from us. |