简体   繁体   English

在字符串中找到的正则表达式替换匹配项

[英]Regex Replace Match Found in String

I am using a Regex to find a pattern within a string. 我正在使用正则表达式在字符串中查找模式。 After I find a match I would then like to remove it from the existing string. 找到匹配项后,我想将其从现有字符串中删除。

For example: 例如:

starting string ex: "Billed Hours (.5);" 
converted string: "Billed Hours"

The regex pattern I am using is @"([az.]+);$" - Not sure if this is correct, I want to check that only whole numbers or decimals are within the parentheses. 我正在使用的正则表达式模式为@“([[az。] +); $”-不知道这是否正确,我想检查括号中是否只有整数或小数。 If the pattern matches then remove parentheses, the value inside as well as the following semi-colon. 如果模式匹配,则删除括号,里面的值以及以下分号。

This is what I have so far: 这是我到目前为止的内容:

string testString = "Billed Hours (.5);"
testString = Regex.Replace(testString, @"\([a-z.]+\);$", "");

This is in C#. 这是在C#中。

Any ideas? 有任何想法吗?

考虑...

string testString= Regex.Replace(testString, @"\(\d*\.?\d*]*\);$", "");

Try the regex: 试试正则表达式:

@"\s+\([^)]+\);$"

And replace by nothing. 并一无所获。

The \\s+ is to avoid having to trim spaces and [^)]+ matches any character except closing paren. \\s+是为了避免必须修剪空格并且[^)]+匹配除封闭括号外的任何字符。

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

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