Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

Comment on Quick Regex quesiton
Download Code
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 !

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 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 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";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://898406]
Approved by chrestomanci
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2014-07-24 05:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (157 votes), past polls