Search code examples
regexstringparentheses

REGEX match string include content inside optional delimiter


I am trying to match a string using regex and I am very close to having it working the way I want.

Lets say I have a string 5A(test1),4B,3C(test2)

The first thing I do is break the string apart on the commas, so i end up with 3 strings in an array

  • 5A(test1)
  • 4B
  • 3C(test2)

Now I want to pull the following information out; the digit, the letter and the content in the parentheses. But the parentheses are optional.

Here is my pattern ([1-9][0-9]*)([AaBbCcIiFfPpSs]+)(\(.*\))?

This works except that it includes the parentheses. so i get
5 A (test1)
when what i want is
5 A test1

Ive also tried ([1-9][0-9]*)([AaBbCcIiFfPpSs]+)\(([^)]*)\)?
But this doesn't match on the strings without the parentheses so
5A(test1) and 3C(test2) match but 4B does not.

Any assistance would be appreciated.


Solution

  • Change your regex a bit:

    ([1-9][0-9]*)([AaBbCcIiFfPpSs]+)(\((.*)\))?
    

    The content inside () will be in capturing group 4.

    If your language supports non-capturing group (?:pattern):

    ([1-9][0-9]*)([AaBbCcIiFfPpSs]+)(?:\((.*)\))?
    

    This will prevent unnecessary capturing (saves some memory), and the content inside () will be in capturing group 3.