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

Question about parentheses in regex

by Von_Halen (Novice)
on May 27, 2015 at 19:07 UTC ( [id://1128060]=perlquestion: print w/replies, xml ) Need Help??

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

Oh wise seekers of the Perl Wisdom, I come seeking help. This is, I am sure, a novice question and most likely a stupid one... But I've tried everything I can think of and can't seem to get this to work. I ran into an issue where words in parentheses are not being removed from a regex as wanted... The goal is to have anything inside of the curly braces (including the curly braces themselves) to disappear from the string. This works for all circumstances except for when there is a set of parentheses in the text.. I tried escaping the parentheses "\( and \)", but it was to no avail.. I understand that the parentheses are grouping tools in regex, but I figured escaping them would fix that problem, and it did not...

my $text = "to { (This Word) }"; my $remove = "{ (This Word) }"; $text =~ s/\R//g; $text =~ s/$remove//; print $remove, $/, $/; print $text;
Output:
{ (This Word) } to { (This Word) }
Looking for someone to shed some light on the situation, as it's holding up my production. Again, I'm sorry for I'm sure this is a simple thing to fix..

Replies are listed 'Best First'.
Re: Question about parentheses in regex
by toolic (Bishop) on May 27, 2015 at 19:14 UTC
    quotemeta
    use warnings; use strict; my $text = "to { (This Word) }"; my $remove = quotemeta "{ (This Word) }"; $text =~ s/\R//g; $text =~ s/$remove//; print $remove, $/, $/; print $text; print "\n"; __END__ \{\ \(This\ Word\)\ \} to
      Oh my god, I love you. Thank you very much. What does the quotemeta do in this case?
        my $remove = quotemeta "{ (This Word) }"; print $remove; # See what happened.

        You can also shorten it to

        my $text = 'to { (This Word) }'; my $remove = '{ (This Word) }'; $text =~ s/\R//g; $text =~ s/\Q$remove//; # <-- the \Q means quotemeta
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        "What does the quotemeta do...?"

        That's thoroughly explained in the docs. Ask your Teddy Bear ... uh, no, I mean, ask your machine's command line interface (using perldoc perlop [look for "Quote and Quote-like Operators"] or ask your browser using a search engine for http://perldoc.perl.org/functions/quotemeta.html which will tell you all about it.

        We value sell-help self-help very highly here in the Monastery. You'll also find interesting (and helpful) information about site's expectations of those asking questions at PerlMonks FAQ section.

        Edited: Typo fixed. TY soonix for catching what I should have.

Re: Question about parentheses in regex
by Sathishkumar (Scribe) on May 28, 2015 at 06:39 UTC
    Hi, check with below code.
    my $text = "to { (This Word) }"; my $remove = "{ (This Word) }"; $text =~ s/\Q$remove\E//; print $remove, $/, $/; print $text;
    Regards, Sathishkumar S

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-19 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found