Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

regex related...search and delete

by freekngeek (Acolyte)
on Jan 14, 2013 at 10:19 UTC ( #1013180=perlquestion: print w/ replies, xml ) Need Help??
freekngeek has asked for the wisdom of the Perl Monks concerning the following question:

Hey all, I am working on a text file and I want to delete some specific letter, Like L & W which stand for length and width. For an example I have (L 2 W 7), but I want to have only values inside the brackets. I couldnt find any answer by myself. So I hope if you could tell me the way or is there any way to do this ?

Comment on regex related...search and delete
Re: regex related...search and delete
by choroba (Abbot) on Jan 14, 2013 at 10:32 UTC
    You can use a simple substitution. [LW] is a character class containing both L and W, the final /g is needed to replace both the letters.
    $string = "(L 2 W 7)"; $string =~ s/[LW] //g; # Replace L or W followed by a space by nothing +. print $string, "\n";
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thanks a lot man. it's working :)
Re: regex related...search and delete
by The Perlman (Beadle) on Jan 14, 2013 at 10:33 UTC
    bla ...

    depending on your textfile this can be indefinitely complex.

    are parens ever nested?

    is it always (L \d+ W \d+)?

    If it's not a fix format I would try to match every parens-pair, and call a function with the match with /e modifier. s/\(.*?\)/kill_LW($&)/ge

    - Ron
      It's a fix format and it's working now. Thank you for your solution, May be I'll try this some other time.
        > It's a fix format and it's working now.

        no it's not, chorobas solution will delete any "L " outside of parens too.

        if its really a fix format, use something like s/\(L (\d+) W (\d+)\)/($1 $2)/g

        Cheers Rolf

Re: regex related...search and delete
by Anonymous Monk on Jan 14, 2013 at 10:45 UTC
Re: regex related...search and delete (m/\G..//gcx)
by Anonymous Monk on Jan 14, 2013 at 11:24 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2014-09-18 21:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (124 votes), past polls