in reply to Re: Regexp, match the nearest verb.
in thread Regexp, match the nearest verb.

I see. Thank you!! But if I match the other way round, let say I have such a sentence: $sentence = "Boy NN wants VB to TO take VB note NN"; I want to match the nearest verb before the noun "note NN". the code is like this: ============================ $phrase = "note NN"; if ($sentence =~ /(\w+\sVB)*(.|\n)*?($phrase)/ { $node = $1.$3; print $node; } ============================ However, this gives me $1 = wants, instead of the one I want: "take". Is there any way to solve this problem? Besides, how to match the regexp from the end the string? Thanks!!
  • Comment on Re: Re: Regexp, match the nearest verb.

Replies are listed 'Best First'.
Re: Re: Re: Regexp, match the nearest verb.
by ariels (Curate) on Dec 28, 2001 at 17:55 UTC

    Ah. What happens is that you still get the first match in the string. In this case, that's the match starting at "<samp>wants VB</samp>" (what's the `*' doing after `VB)' in your regexp, anyway?). You could use greediness to hit the last match in the string:

    $sentence =~ /.*(\w+\sVB)(.|\n)*?($phrase)/
    But that still doesn't guarantee "closenessest", just "lastness".

    Your alternative is to look up and use sexegers for this second task. You will find much information on this very site...


    PS. Please try to format your writeups so we can read them...