简体   繁体   中英

How to remove numbers in string containing a specific identifier with regex

I'm stuck and hoping one of you regex wizards has insight. I need to remove anything in a string that contains "PK##.##". So for example:

string ts = "HELLO PK 2233.33 TEST PK11.1";
//Should output as "HELLO PK 2233.33 TEST"

string ns = Regex.Replace(ts, @"PK[\d]", string.Empty); 
//currently outputting "HELLO PK 2233.33 TEST 1.1"

My current Regex almost works, but only removes 1 digit from the "PK" part of the string.

You need to match trailing whitespaces and fractional part as well:

\s+PK\d+(?:\.\d+)?

If \\.\\d+ part is mandatory remove the enclosing parentheses:

\s+PK\d+\.\d+

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