Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Str extract the word between the first and the 2nd '|' character

by dani_cv (Acolyte)
on Apr 11, 2008 at 14:00 UTC ( [id://679686]=perlquestion: print w/replies, xml ) Need Help??

dani_cv has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Can anyone please help...

I want to extract 'No Qualifier' from the below string

my $string = '(<some word> | No Qualifier | <some word> | <some word>';

Here the <some word> is dynamic, I want to extract the word between the first and the 2nd '|' character basically..
Thanks in advance,
Dan.
  • Comment on Str extract the word between the first and the 2nd '|' character

Replies are listed 'Best First'.
Re: Str extract the word between the first and the 2nd '|' character
by FunkyMonk (Chancellor) on Apr 11, 2008 at 14:09 UTC
    my ( $second_word ) = $string =~ m{\|([^|]*)\|};

    Or, in more detail:

    my ( $second_word ) = $string =~ m{ \| # "|" escaped because it's special inside a regexp ( # start capturing [^|]* # some not |'s ) # end capture \| # another "|" }x;

      Wow...so quick...thank you...

      I just added \s+ to trim the white spaces and this looks perfect..thank you indeed...
      my ( $second_word ) = $string =~ m{\|\s+(^*)\s+\|};

      Dan.
        What is the difference between declaring the variable from normal declaration "my $second_word" from the below declaration?

        my ( $second_word )
        Thanks,
        Dan.
Re: Str extract the word between the first and the 2nd '|' character
by johngg (Canon) on Apr 11, 2008 at 14:31 UTC
    To expand on Anonymonk's reference to split and incorporating your whitespace modification to FunkyMonk's solution.

    my $secondWord = ( split m{\s*\|\s*}, $string )[1];

    Six of one, half a dozen of the other, really!

    Cheers,

    JohnGG

      Thank you!
Re: Str extract the word between the first and the 2nd '|' character
by radiantmatrix (Parson) on Apr 11, 2008 at 16:41 UTC

    Well, the regex and split will certainly work. I'd like to provide two more options for you.

    The first uses index and substr. This has the advantage of being very fast (often faster than regex -- regexes can do more, though). I don't know if it's faster than split (probably not), but there may be reasons for you to use it.

    my $string = '(<some word> | No Qualifier | <some word> | <some word>' +; my $start = index( $string, '|' ); # find first pipe position my $end = index( $string, '|', $start+1 ); # find next pipe position my $qualifier = substr( $string, $start, $end-$start+1 ); # extract

    The second suggestion I have is a bit more robust, and leverages the module Text::CSV_XS. You'd use this approach if you have, e.g. a file of lines that look like your example -- and especially if you're interested in more than one field in that line, and/or if your data might have escaped pipes in it.

    use IO::File; use Text::CSV_XS; my $csv = Text::CSV_XS->new({ sep_char=>'|' }); my $io = IO::File->new('myfile.piped.txt', '<') or die "Can't read fi +le: $!\n"; until ($io->eof) { my $row = $csv->getline($io); # read and parse line into an ARRAY +ref print "Second field says: ", $row->[1], "\n"; }

    Even if your data isn't in a file, Text::CSV_XS can parse it for you; it's worth looking into.

    <radiant.matrix>
    Ramblings and references
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet
Re: Str extract the word between the first and the 2nd '|' character
by Anonymous Monk on Apr 11, 2008 at 14:07 UTC
    perldoc -f split

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-25 19:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found