Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

How can I extract part of a string after a specific character

by horgof (Initiate)
on Jul 11, 2002 at 04:38 UTC ( #180940=perlquestion: print w/replies, xml ) Need Help??

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

I have a line like this:

TITLE=SPECIAL CASE 1

How can I extract part of this string (the string after equal "="). After extraction:

$string = "SPECIAL CASE 1"

  • Comment on How can I extract part of a string after a specific character

Replies are listed 'Best First'.
Re: How can I extract part of a string after a specific character
by DamnDirtyApe (Curate) on Jul 11, 2002 at 05:13 UTC

    Here's two ways:

    my $str = 'TITLE=SPECIAL CASE 1' ; # Using a regexp... $str =~ /^(.*?)=(.*?)$/ ; my $match = $2 ; print "Matched: [$match]\n" ; # Using split... my ( $before, $after ) = split '=', $str ; print "Matched: [$after]\n" ;

    _______________
    D a m n D i r t y A p e
    Home Node | Email
Re: How can I extract part of a string after a specific character
by weini (Friar) on Jul 11, 2002 at 05:21 UTC
    #!/usr/bin/perl -w use strict; my $string = "TITLE=SPECIAL CASE 1"; $string =~ /.*=(.*)/; print $1;

    . stands for any character but a newline
    * for zero or more times
    () stores the result in $1.
    Type "perldoc perlfaq6" for more details ...

    hth
    weini

      Both you and DamnDirtyApe have a .* preceeding the =. Why?
      print $string =~ /=(.*)/
      will do fine.

      Abigail

        will do fine.

        But not the same. Their versions grab everything after the last =, your version grabs evertyhing after the first one.
        I think your solution is better (and I'm sure it's a lot more efficient), but we can't know for sure without more information.

        $\ = "\n"; for ('title=foo' ,'title=foo=bar') { print "-> $_"; print /.*=(.*)/; print /=(.*)/; }

        - Yes, I reinvent wheels.
        - Spam: Visit eurotraQ.
        

Re: How can I extract part of a string after a specific character
by greenFox (Vicar) on Jul 11, 2002 at 05:28 UTC

    I suspect split would be your best choice-

    # split my ($key, $string1) = split /=/, $_, 2; print "$key, $string1\n";

    Though there are other ways-

    #re 1 /^TITLE=(.*)/; my $string2 = $1 || ''; print "$string2\n"; #re 2 (my $string3 = $_) =~ s/TITLE=//; print "$string3\n"; # substr my $string4 = substr($_, 6); print "$string4\n";

    Just as examples. Which method you choose will depend on what else is going on in your program and what the most general case of your expression is. If it is *always* TITLE="some string" I might use substr. If it was (for example) "some text in upper followed by an equals followed by some string ending in a numeric" *and* I had to ignore other lines that include "=" I might use an regular expression.

    --
    my $chainsaw = 'Perl';

Re: How can I extract part of a string after a specific character
by bronto (Priest) on Jul 11, 2002 at 12:15 UTC
    my $string ; my $title = 'TITLE=SPECIAL CASE 1' ; $string = $title if $title =~ s/^TITLE=// ;

    Now $string is SPECIAL CASE 1. If $title didn't match, it is undef, and you can test it with a simple if ($string) {..., for example (but I would rather use if (defined $string) {...)

    Ciao!
    --bronto

    # Another Perl edition of a song:
    # The End, by The Beatles
    END {
      $you->take($love) eq $you->made($love) ;
    }

Re: How can I extract part of a string after a specific character
by caedes (Pilgrim) on Jul 11, 2002 at 07:01 UTC
    Here is a crazy way to do it:
    my $test = "TITLE=SPECIAL CASE 1"; my @temp; for(reverse split(//,$test)){ last if /=/; push @temp,($_); } print reverse @temp;

    -caedes

Re: How can I extract part of a string after a specific character
by joshua (Pilgrim) on Jul 11, 2002 at 15:47 UTC
    This'll work...
    use strict; my $string = "TITLE=SPECIAL CASE 1"; $string = (split('=', $string))[1]; print "$string\n";
    Prints SPECIAL CASE 1.

    Joshua

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2023-01-31 06:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?