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.


In reply to Re: Regex help by graff
in thread Regex help by newbio

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.