Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regexp with round brackets

Status
Not open for further replies.

domenicodl

Programmer
Jul 2, 2011
15
IT
Hi,
I want to replace "(" and ")" with "". Suppose that you have a string like 00(r1) I would like to have 00r1.
To this end, I wrote:

regexp = "[\)\)]";
sub(regexp,"",r);

where r is the variable where I store the string 00(r1).
With the former code, I get 00(r1) -> 00(r1
I also tried with
regexp = "/[\(\)]/"
with the same result.
Thank you in advance
 
Hi
[ul]
[li]No need to escape parenthesis ( () ) inside the character list.[/li]
[li][tt]sub()[/tt] will replace only the first one, use [tt]gsub()[/tt] for multiple replacements.[/li]
[/ul]
Code:
regexp="[()]"
gsub(regexp,"",r[i]))

[gray]# or[/gray]

gsub(/[()]/,"",r[i])
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].


Feherke.
 
One way is to use 2 sub statements
Code:
sub(/\(/,"",r[i])
sub(/\)/,"",r[i])


CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top