REGEX match string include content inside optional delimiter -


i trying match string using regex , close having working way want.

lets have string 5a(test1),4b,3c(test2)

the first thing break string apart on commas, end 3 strings in array

  • 5a(test1)
  • 4b
  • 3c(test2)

now want pull following information out; digit, letter , content in parentheses. parentheses optional.

here pattern ([1-9][0-9]*)([aabbcciiffppss]+)(\(.*\))?

this works except includes parentheses. get
5 (test1)
when want is
5 test1

ive tried ([1-9][0-9]*)([aabbcciiffppss]+)\(([^)]*)\)?
doesn't match on strings without parentheses so
5a(test1) , 3c(test2) match 4b not.

any assistance appreciated.

change regex bit:

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

the content inside () in capturing group 4.

if language supports non-capturing group (?:pattern):

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

this prevent unnecessary capturing (saves memory), , content inside () in capturing group 3.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -