0

I have a string which contains word “Limited” or “Ltd”. My requirement is to remove any text in brackets "( )" after the same words “Limited” or “Ltd” in the given input string.

For example "Abcd Ltd (North)" will become "Abcd Ltd" but “Abc (North)” will remain “Abc (North)”

Also "ABCD Ltd test (North)" will remain same as it is.

I am trying to find out regular expression in c#, which can solve the above issue?

Any help is appreciated.

4
  • are you familiar with the string.Replace() function.. also those are parenthesis not brackets anyway you can also you can look up how to use the IndexOf() method as well this is actually a very simple thing to do look up string.Contains as well Commented Sep 8, 2014 at 14:43
  • possible duplicate of Replace first occurrence of pattern in a string Commented Sep 8, 2014 at 14:45
  • Regex.Replace Function Commented Sep 8, 2014 at 14:45
  • I need to find bracket immediately after the word "Limited" or "ltd" there can or can't be space between the word and space. How to do that. Commented Sep 8, 2014 at 14:47

1 Answer 1

3

In that case I would use Lookbehind.

The use would be like this:

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

And use Regex.Replace to remove the text.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, It does work . But when there are more than 1 space between word "Limited" and the bracket it is not working. For example "ABC ltd (345)" is not getting replaced, as there are more than 1 space between ltd and (345), I need to suppress all the spaces between them.
in that case replace \s? with \s*?.
If the solution has worked please accept the answer :)
unfortunately the question marked as 2 vote down, so I cant accept the answer even It does work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.