1

I have two lists of strings with some differing elements, and some repeated elements. However, the repeated elements differ in the second characeter e.g.

ListA=[MaB123, MaD789, DaB123]

ListB=[MbB123, MbQ789, DbB123]

I want to check each element of List A for repetitions in ListB.

In the above, I would want:

ListA[0] match ListB[0]

ListA[1] no match

ListA[2] match ListB[2]

I am trying to do this using the re library.

I intend to write a for loop to check each element of ListA and test for a match with an element in ListB (perhaps inefficient, but just trying to get my head around regex at the moment), but I am stuck at the stage of getting the regular expression that I would need to use.

So let us assume

StringA=listA[0]='MaB123'

StringB=listB[0]='MbB123'

I want to build the regex using string slicing so that I can build the regex dynamically in the for loop.

This is what I have tried

m=re.search(r'(stringA[0])[ab](stringA[2:])',stringB)
display(m)

But it returns "none"

While

m=re.search(r'([M])[ab]([B123])',stringB)
display(m)

returns a match. What is the difference between the first and second expression, and how can I build the regex using string slicing ?

Many thanks

IrisOren
  • 69
  • 1
  • 1
  • 6
  • You need to use string concatenation so for example ```m=re.search('('+stringA[0]+')[ab]('+stringA[2:]+')',stringB)``` – lc74 Jun 24 '19 at 13:55

1 Answers1

0

You should be using f-strings or formatting. Your current regex does not contain your variables.

m=re.search(r'(stringA[0])[ab](stringA[2:])',stringB)

to this

m=re.search(rf'({stringA[0]})[ab]({stringA[2:]})',stringB)
vahvero
  • 525
  • 11
  • 24