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

regex 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 and delete
Replies are listed 'Best First'.
Re: regex and delete
by choroba (Canon) 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 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 and delete
by Anonymous Monk on Jan 14, 2013 at 10:45 UTC
Re: regex and delete (m/\G..//gcx)
by Anonymous Monk on Jan 14, 2013 at 11:24 UTC

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2015-08-31 00:20 GMT
Find Nodes?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...

    Results (351 votes), past polls