简体   繁体   中英

Python: Using two variable strings as Regex.findall criteria

What's up guys, I try to receive two strings, and try to find every text padded by the two strings. For example:

Pattern_text = '\|\|####\|\|' #To be received from user
first_half= Pattern_text [0:len(Pattern_text )//2]  # to get left side of pattern \\##
second_half= Pattern_text [len(Pattern_text )//2:]  # to get right side of pattern ##\\
#Sample text
SEARCH_ME = r"BLABLABLA BLA ||##MATCH_ID_1548##|| BLA ||##MATCH_ID_3412##|| BLABLABLA"
#Trying to find all matches padded by the two halves
results = re.findall((first_half+ r'(.*?)'+second_half), SEARCH_ME)
print(results)

results is always empty in this case. expected results should be a list whose elements are 'MATCH_ID_1548'and'MATCH_ID_3412'

What do you reckon I should do with those two string variables, knowing that I'm sure the regex of the concatenated string (first_half + '(.*?)' + second_half) works when hardcoded. But not in that format.

Thanks in advance.

Here's a quick example of what's working on my end. We would be able to help you a lot more if you provide a complete and reproducible example in the question.

import re

prefix = "prefix #"
suffix = "# suffix"

regex = fr"{prefix}(.*?){suffix}"  # Renders as "prefix #(.*?)# suffix"
test_string = "prefix #TEST STRING# suffix"

print(re.findall(regex, test_string))  # ['TEST STRING']

Here is the another way. It will also work on cases where your 4 digit match id scales to 5 digit or more.

import re

SEARCH_ME = r"BLABLABLA BLA ||##MATCH_ID_1548##|| BLA ||##MATCH_ID_3412343434##|| BLABLABLA"

print(re.findall(re.compile('(?:MATCH_ID_[0-9]{4,})'), SEARCH_ME))

Result:

['MATCH_ID_1548', 'MATCH_ID_3412343434']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM