Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

How to delete letters and special characters from a steering

by sharifeee01 (Initiate)
on Mar 05, 2014 at 01:30 UTC ( [id://1077033]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I need to delete letters and special characters from a steering using perl. The steering is like "name=0.5e-7". How could I keep the numerical part of the string by deleting other characters. Thanks
  • Comment on How to delete letters and special characters from a steering

Replies are listed 'Best First'.
Re: How to delete letters and special characters from a steering
by davido (Cardinal) on Mar 05, 2014 at 02:30 UTC

    Does your data's format have a name? Maybe there's already a tool that correctly interprets it.

    Is your data always in the form of identifier=number? In such a case, split (possibly (split /=/, $string, 2)[1]) or m/^[^=]+([\de.+-]+)$/

    Is the number always in scientific notation?

    Specifics lead to better answers.


    Dave

Re: How to delete letters and special characters from a steering
by Kenosis (Priest) on Mar 05, 2014 at 01:39 UTC

    What have you tried?

    Consider either splitting the string on = and just use the second element from the split or use a regex to capture all after the =.

    s/steering/string/g ?

Re: How to delete letters and special characters from a steering
by AnomalousMonk (Archbishop) on Mar 05, 2014 at 18:00 UTC

    An alternative approach to eliminating everything you don't want from a string is to capture only what you do want: it's often easier to define what you want than what you don't. Consider:

    c:\@Work\Perl>perl -wMstrict -le "use Regexp::Common qw(number); ;; my $str = 'name=0.5e-7,noodle=123.0e5,niddle=1.2,noddle=9'; ;; my $number = qr{ (?<! \d) $RE{num}{real} (?! \d) }xms; ;; my @numbers = $str =~ m{ $number }xmsg; printf qq{'$_' } for @numbers; " '0.5e-7' '123.0e5' '1.2' '9'

    See Regexp::Common.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-18 14:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found