简体   繁体   中英

C# Regex - Need to remove parentheses

I'm looking for a C# regex for replacement to take this:

HP06B66F (HP Officejet Pro 8600) (redirected 4)

to

HP06B66F (HP Officejet Pro 8600)

I am using the regex below and it gets me to HP06B66F (HP Officejet Pro 8600) ()

(redirected \\\\d*)

I need to get it to remove that last () not just what is in between.

Thanks for the help

Why do you need regex for that? You can do it like this:

string initialString = "HP06B66F (HP Officejet Pro 8600) (redirected 4)";
string wantedString = initialString.Remove(initialString.LastIndexOf("(")).TrimEnd();

Result:

结果图片

You could also try with any of these, all giving the same result:

string wantedString = initialString.Replace(" (redirected 4)", "");
string wantedString = Regex.Replace(initialString, @"\(redirected \d*\)", "").TrimEnd();

Change your regex to "\\(redirected \\d*\\)" to treat the () as normal characters.

(In C# depending on how you're doing this it might have to be \\\\( and \\\\) because you have to deal with the string's escape characters first)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM