how do I use Perl to get rid of text within parentheses? For example:
$str = “This is a (extra stuff) string.”
to
$str = “This is a string.”
I am current using this but it’s not working:
$str =~ s/( ( [^)]+ ) )//;
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You need to escape the parentheses, like:
Update by popular demand:
To remove the space you can simply remove spaces before the parenthesis. This will work in most cases:
To handle nested parenthesis you can use a recursive pattern, like so:
You can read about
(?R)and the like in perlre.The last expression will work for string like
aaa (foo(b,a,2*(3+4)) b) (c (c) c) ddd (x)., givingaaa ddd..