I have a bunch of strings that may contains certain patterns. Specifically, the following 3.
Starts with
(-
followed by 10 digits followed by)
.E.g.:
(-1234567890)
Starts with
(
, ends with)
, and may contain 1 or more characters, but NO spaces.E.g.:
(ABC) or (AF33) or (2345)
Starts with
(
, ends with)
, and may contain 1 or more characters, INCLUDING spaces.E.g.:
(Some string)
The strings I work with may contain zero or more of the patterns above. My requirement is to match ONLY the second one from above in a given string, and I'd like to be able to use Regex
class in C#.
For example, let's say following are five different strings I have.
This is some random text.
This is some (ABC) random (-1234567890) text.
This is some (XY12) random (-1234567890) text.
This is some (Contains space) random (-1234567890) text.
This is some () random text.
My Regex
should match only the 2nd and 3rd strings from the above list.
So far, I've managed to write this following Regex, which excludes strings 1 and 5.
.*\((?!\-).+\).*
This matches 2nd, 3rd, AND 4th strings above. Now I'm not sure how I can get it to exclude the 4th, one which contains spaces inside parenthesis. I know that \S
detects whitespaces, but how can I tell it to detect strings that do not contain spaces only within the parenthesis that don't contain a -
after the first (
?
EDIT 1:
There will never be nested parenthesis in my strings.
EDIT 2:
Here's a Regex Tester.