Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Regex Help

by Anonymous Monk
on Mar 28, 2013 at 05:20 UTC ( #1025853=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hiya Monks,

How can I delete the last digit part from a line. For example,

$line= "piratesofcareebian100"; $line= "pirates of careebian100"; $line= "pirates of careebian 100";

I only need from "pirates" upto "careebian".

How can I have only the alphabet part and delete the digit part using a regex.

Thank you.

Comment on Regex Help
Download Code
Re: Regex Help
by 2teez (Priest) on Mar 28, 2013 at 05:42 UTC

    How would you write it?
    Try something like so:

    use strict; use warnings; while(<DATA>){ chomp; print $1,$/ if/(\D+?)\d+?/; } __DATA__ piratesofcareebian100 pirates of careebian100 pirates of careebian 100
    Please also check these documentation perlre, perlrequick and perlretut

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      Thanks 2teez, Now I have 1 more doubt. pls can u? If the data is like below, how we can do it?
      $line = "pirates1 of careebian 100"; $line = "pirates2 of careebian 100"; $line = "pirates3 of careebian 100";
      and I need the output as "pirates1 of careebian" & "pirates2 of careebian" & "pirates3 of careebian"

        then change this print $1,$/ if/(\D+?)\d+?/;
        to
        this print $1,$/ if/(.+?)\s+?\d+?/;

        UPDATE:
        In case, you have all these data in the same file you can try this:

        use strict; use warnings; while(<DATA>){ chomp; print $1,$/ if/(.+?)(\s+)?\d+?$/; } __DATA__ piratesofcareebian100 pirates of careebian100 pirates of careebian 100 pirates1 of careebian 100 pirates2 of careebian 100 pirates3 of careebian 100

        Output
        piratesofcareebian pirates of careebian pirates of careebian pirates1 of careebian pirates2 of careebian pirates3 of careebian
        Please, check the reference I mentioned in my first post on this thread.
        Hope that helps

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (14)
As of 2014-09-30 16:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (378 votes), past polls