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

String extract 2

by kepler (Scribe)
on Jul 24, 2014 at 10:55 UTC ( #1094887=perlquestion: print w/replies, xml ) Need Help??
kepler has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm having some troubles to extract an expresiion from a $line. The type of expression is, for example:
KP_what_I_want (space) or KP_what_I_want( or KP_what_I_want ( or KP_what_I_want + etc...
I'm using:
my ($exp) = $line =~ m/ KP_ # Required (.*) # Capture Desired Output (?:\s)? # Optional - Do not capture (?:\()? # Optional - Do not capture /xi;
It's not working... it extracts the all expression ( without the KP_) Any ideas? Thanks, Kepler

Replies are listed 'Best First'.
Re: String extract 2
by farang (Chaplain) on Jul 24, 2014 at 12:35 UTC

    You can use a negated character class to match up until the included chars in the class are found.

    #!/usr/bin/env perl use strict; use warnings; use feature 'say'; my @test_cases = ( 'KP_what_I_want(but_not_this', 'KP_what_I_want nor_this', 'KP_ nor_any_of_this', 'and this will not match', ); for my $line (@test_cases) { my ($exp) = $line =~ m/ KP_ # Required. ( [^ (]* ) # Capture until space # or left paren. /xi; if (defined $exp) {say "<$exp>"} else {say 'NO MATCH'}; } __END__ <what_I_want> <what_I_want> <> NO MATCH

Re: String extract 2
by jellisii2 (Hermit) on Jul 24, 2014 at 12:03 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2019-02-21 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I use postfix dereferencing ...









    Results (108 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!