Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
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 " }

Replies are listed 'Best First'.
Re: regexp to grab STRING...[everything here]...DIF STRING
by frozenwithjoy (Priest) 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 (Canon) 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?
[Discipulus]: but the best i attended live was Mano Negra Patchanka at Forte Prenestino .. in 1990
[Corion]: Hmmm - Mano Negra or at least Manu Chao seem to put on a good live show. At least the one live CD I have from Manu Chao sounds good ;)
Discipulus feels the same jealousity of the johngg's daughter
[1nickt]: choroba I agree
[choroba]: Playing in a punk rock band for 20 years... my hearing is quite bad
[Corion]: I still have hopes to turn my godson and his two siblings into a punk band ;)
[Corion]: Their older sister just started piano but has been interested in drumming, which she should be able to start with 8 years or so)
[Discipulus]: ah was Kink of Bongo 1992..
[1nickt]: choroba name of band? youtube link? MySpace link?

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2017-03-24 12:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (301 votes). Check out past polls.