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.

user1899731
  • 61
  • 2
  • 5
  • 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 – MethodMan Sep 08 '14 at 14:43
  • possible duplicate of [Replace first occurrence of pattern in a string](http://stackoverflow.com/questions/8809354/replace-first-occurrence-of-pattern-in-a-string) – MethodMan Sep 08 '14 at 14:45
  • [Regex.Replace Function](http://msdn.microsoft.com/en-us/library/haekbhys.aspx) – MethodMan Sep 08 '14 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. – user1899731 Sep 08 '14 at 14:47

1 Answers1

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.

Suhjin Park
  • 440
  • 3
  • 11
  • 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. – user1899731 Sep 08 '14 at 15:00
  • in that case replace \s? with \s*?. – Suhjin Park Sep 08 '14 at 15:02
  • If the solution has worked please accept the answer :) – Suhjin Park Sep 08 '14 at 15:05
  • unfortunately the question marked as 2 vote down, so I cant accept the answer even It does work. – user1899731 Sep 08 '14 at 15:06