Home > Back-end >  Regular expression to find comma between brackets
Regular expression to find comma between brackets

Time:10-09

I want to write a regex expression which will tell if there is any comma between brackets ().

CASE 1: Input string: MNO (ABC,PQR,XYZ)

OUTPUT: TRUE

CASE 2: khgdfkhgkfhgkdgk

OUTPUT: FALSE

Please let me know any article which i can refer to achieve this requirement

CodePudding user response:

string = 'abc(a,b,c)abc'
substring = string.split('(')[1].split(')')[0]
print(substring)
if ',' in substring:
    print(True, substring.count(',')) # if you want the count you can do this
else:
    print(False)

CodePudding user response:

.*\(.*,.*\).*

Test it out here.

CodePudding user response:

You could use python's inbuilt re module. Below is sample code of how you could do it:

import re
sample1 = 'MNO (ABC,PQR,XYZ)'
sample2 = 'khgdfkhgkfhgkdgk'

print(re.search(r"\(.*,.*\)", sample1)) // --> Prints a match
print(re.search(r"\(.*,.*\)", sample2)) // --> Prints None
  • Related