1

So I have a regex pattern:

no = 'C 030234(A) ZMM' 
pattern = '\d{1,2}(\s*)' + no

What I need to do is add a backslash before the ')' and '(' characters in the variable, no, to make the literals.

I tried something like

tList = []
for c in no:
  if np.logical_or(c=='(',c==')'):
    tList.append('\\'+c)
  else:
    tList.append(c)


no = "".join(tList)
pattern = '\d{1,2}(\s*) ' + no

But this yields a regex pattern of

'\d{1,2}(\s*)C 030234\\(A\\) ZMM'

I need to find away to any string in the form of no, and add a escape character before any parenthesis to make the proper regex pattern

'\d{1,2}(\s*)C 030234\(A\) ZMM'

0 Answers0