Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by weini (Friar)
on Jul 11, 2002 at 05:21 UTC ( [id://180948]=note: print w/replies, xml ) Need Help??


in reply to How can I extract part of a string after a specific character

#!/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

Replies are listed 'Best First'.
Re: How can I extract part of a string after a specific character
by Abigail-II (Bishop) on Jul 11, 2002 at 09:57 UTC
    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.
      

        Their versions grab everything after the last =, your version grabs evertyhing after the first one.
        Eh, no. Only *one* of their versions grabs everything after the last, the other grabs everything after the first. The problem however didn't specify what should happen if there are multiple = characters present.

        Abigail

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-28 09:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found