简体   繁体   English

如何使用正则表达式删除包含特定标识符的字符串中的数字

[英]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##.##". 我需要删除包含“ 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. 我当前的Regex几乎可以使用,但是仅从字符串的“ PK”部分删除了1位数字。

You need to match trailing whitespaces and fractional part as well: 您还需要匹配尾随空格和小数部分:

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

If \\.\\d+ part is mandatory remove the enclosing parentheses: 如果必须使用\\.\\d+部分,请删除括号:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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