0

I am trying to make a script that reads the list element sequentially and concatenate them each into regex pattern.

for example,

I have a list like this:

lst = ['B.', 'Article', 'III']

And want to comprise regex something like this:

re.search(r lst[0]\s+lst[1]\s+lst[2]).group()

so that it can match below regardless of white_spaces between each elements from the list:

candidate_1 = 'B.      Article III'
candidate_2 = 'B.        Article III'
4
  • 2
    Ok. We wish you the best of luck Commented Feb 21, 2018 at 6:57
  • @sshashank124 thx I really hope so :D Commented Feb 21, 2018 at 6:57
  • 1
    What is your question? Commented Feb 21, 2018 at 6:58
  • @Robᵩ I don't know how to concatenate r with str. if I do r lst[0]\s+lst[1]\s+lst[2] it reutrns error, and if I do rlst[0]\s+lst[1]\s+lst[2] it also definitely returns error Commented Feb 21, 2018 at 7:02

1 Answer 1

3

Try str.join(), like so:

r'\s+'.join(lst)

Here is a complete program:

import re

def list2pattern(l):
    return r'\s+'.join(l)

lst = ['B.', 'Article', 'III']
assert re.search(list2pattern(lst), 'B. Article III')
assert re.search(list2pattern(lst), 'B.      Article III')
assert not re.search(list2pattern(lst), 'B.Article III')
assert not re.search(list2pattern(lst), 'George')
Sign up to request clarification or add additional context in comments.

4 Comments

I have more fundamental question. I have tried type(list2pattern(lst)) and it returns str, where my question is then, how the machine distinguish between str and r str if they are all memorized as str?
@delinco: What are "str" and "r str"? Ah, probably you mean r"": docs.python.org/3/reference/….
the r that always added before expressing regex pattern
@delinco - See this question: stackoverflow.com/questions/2081640/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.