Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Quick Regex quesiton

by eversuhoshin (Sexton)
on Apr 08, 2011 at 17:52 UTC ( #898406=perlquestion: print w/replies, xml ) Need Help??
eversuhoshin has asked for the wisdom of the Perl Monks concerning the following question:

Hello dear monks~

I have a very quick question. I am looking for accounting policies and I would like to take the following three sentences after following the match.

Here is my code.

my $test='deferred gross profit is blah blah blah.'; if($test=~m/(\bdeferred|unrealized)(.*)(\.)/) { print "matched $1 and the rest is $2"; }

This code captures only the first sentence. How can I modify the code so I capture the next three sentences?

Thank you for your time and consideration

Replies are listed 'Best First'.
Re: Quick Regex quesiton
by wind (Priest) on Apr 08, 2011 at 18:03 UTC
    Do you want each sentence in a separate capture? Then the following would work:
    my $test = do {local $/; <DATA>}; if ($test =~ m{((?:deferred|unrealized).*?\.)\s*(.*?\.)\s*(.*?\.)}s) { print "print '$1', '$2', '$3'"; } __DATA__ foo bar. baz biz. deferred gross profit is blah blah blah. more blah. and yet more blah. But not this blah.
    Or if you don't care about them being separate, you could use the following
    if ($test =~ m{(?=deferred|unrealized)((?:.*?\.){3})}s) {
Re: Quick Regex quesiton
by JavaFan (Canon) on Apr 08, 2011 at 23:15 UTC
    Look ma, without modifiers, backtracking, or /.*?/ (the only pattern worse than /.*/):
    /(\b(?:deferred|unrealized)\b)[^.]*\.)([^.]*\.)([^.]*\.)/ and print "Sentence 1: $1; Sentence 2: $2; Sentence 3: $3\n";
Re: Quick Regex quesiton
by jethro (Monsignor) on Apr 08, 2011 at 18:07 UTC

    I assume all the text is stored in $text. So you could use

    if($test=~m/(\bdeferred|unrealized)(.*?\.){1,3}/s) {

    Untested. Try it out. Note the s at the end to make sure that '.' matches also \n. You don't need that if the sentences are all on one line

Re: Quick Regex quesiton
by chakreey (Acolyte) on Apr 08, 2011 at 17:59 UTC
    What are those three sentences you want to capture ? As, I understand, "deferred gross profit is blah blah blah." is sentence number 1. What are sentences 2 and 3 ?

      I just did that as an example. I want to capture what follows. So it would be deferred gross profit blah blah. blah blah. blah. So I want to capture the rest.And I don't know how :(

      Does this work?

      <code> if ($test=~m/(deferred|gross)(.*)(\.)(.*)(\.)(.*)(\.)) { print "$1, $2, $4, $6"; }

      Or is there a more elegant way to do it?

      Thank you so much :)

        Your code doesn't work ! (.*) seems to consider real full stop (.) as any-character.

        btw wind's code works !

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://898406]
Approved by chrestomanci
and the pool shimmers...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2018-04-21 10:51 GMT
Find Nodes?
    Voting Booth?