简体   繁体   English

查找特定的字符串模式并在C#中替换

[英]Find particular string pattern and replace in c#

I have a string which contains word “Limited” or “Ltd”. 我有一个包含单词“ Limited”或“ Ltd”的字符串。 My requirement is to remove any text in brackets "( )" after the same words “Limited” or “Ltd” in the given input string. 我的要求是删除给定输入字符串中相同的“ Limited”或“ Ltd”一词后方括号“()”中的所有文本。

For example "Abcd Ltd (North)" will become "Abcd Ltd" but “Abc (North)” will remain “Abc (North)” 例如,“ Abcd Ltd(北部)”将变为“ Abcd Ltd”,但“ Abc(北部)”将保持为“ Abc(北部)”

Also "ABCD Ltd test (North)" will remain same as it is. 同样,“ ABCD Ltd测试(北部)​​”将保持不变。

I am trying to find out regular expression in c#, which can solve the above issue? 我试图找出C#中的正则表达式,可以解决上述问题?

Any help is appreciated. 任何帮助表示赞赏。

In that case I would use Lookbehind. 在这种情况下,我将使用Lookbehind。

The use would be like this: 使用将是这样的:

(?<=(Ltd|Limited))\s?\(.*?\)

And use Regex.Replace to remove the text. 并使用Regex.Replace删除文本。

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

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