Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

regexp to grab STRING...[everything here]...DIF STRING

by jeffrgsf (Novice)
on Oct 25, 2012 at 21:23 UTC ( #1000945=perlquestion: print w/ replies, xml ) Need Help??
jeffrgsf has asked for the wisdom of the Perl Monks concerning the following question:

This is probably pretty easy, but I can't get it to work:

I want a regexp to grab everything withing a string between "For" and "In", such that this string:

Zelazowa Wola, Symphonic Poem, For Orchestra In B Minor, Op. 37

would return:
For Orchestra

Se puede? (Thanks!)
Ideally, I could keep the "In " in the original string ($work), but I can re-add it if that's not possible using the method I've already started with.

if ( $work =~ /(For #DON'T KNOW WHAT TO PUT HERE )/ ) { $instruments = $1; $instruments =~ s/[()]//g; #remove extra parentheses $work =~ s/$instruments//g; #remove instruments $instruments =~ s/^\s+//; #remove leading spaces $instruments = substr($instruments, 4, 100); #last line removes the substring "For " }

Comment on regexp to grab STRING...[everything here]...DIF STRING
Download Code
Replies are listed 'Best First'.
Re: regexp to grab STRING...[everything here]...DIF STRING
by frozenwithjoy (Curate) on Oct 25, 2012 at 21:29 UTC
    Like this?
    $ perl -E '$string = "Zelazowa Wola, Symphonic Poem, For Orchestra In +B Minor, Op. 37"; say $string =~ m/(For.*) In/;' For Orchestra

    or....

    $ perl -E '$string = "Zelazowa Wola, Symphonic Poem, For Orchestra In +B Minor, Op. 37"; say $string =~ m/(For.*In)/;' For Orchestra In
      I coulda sworn I'd already tried that! Probably had an error due to something else.

      Thanks!!!! I'll make a donation in the next month or so when my next paycheck comes in! This site is da bomb!
Re: regexp to grab STRING...[everything here]...DIF STRING
by AnomalousMonk (Abbot) on Oct 25, 2012 at 23:23 UTC

    Just a cautionary example of greedy  .* versus lazy  .*? regex matching:

    >perl -wMstrict -le "my $s = 'xx FOR foo IN bar baz and FOR fee fie foe IN fum yy'; ;; my @for_in = $s =~ m{ FOR .* IN }xmsg; printf 'greedy: '; printf qq{'$_' } for @for_in; print ''; ;; printf 'lazy: '; @for_in = $s =~ m{ FOR .*? IN }xmsg; printf qq{'$_' } for @for_in; " greedy: 'FOR foo IN bar baz and FOR fee fie foe IN' lazy: 'FOR foo IN' 'FOR fee fie foe IN'

    Also note that in the conditional statement
        if (my @captures = $string =~ m{ pattern }xmsg) {
            do_something_with(@captures);
            }
    if nothing matches (no captures, array empty), the conditional evaluates false and the statement body is not executed.

Re: regexp to grab STRING...[everything here]...DIF STRING
by marquezc329 (Scribe) on Oct 25, 2012 at 22:26 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (20)
As of 2015-07-07 17:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls