M
M
Max Yawo2018-07-04 12:57:39
Python
Max Yawo, 2018-07-04 12:57:39

How to construct a Regex from pieces of a string in python?

I have such substrings and I want to get a regular expression of the form, but I can’t figure out how to glue these lines together so that I can add an escape character \ before + td
b"-"; b"|"; b"+"
rb"[\-|\||\+]+"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nick Sdk, 2018-07-04
@Zlat1997

drive your substrings into an array and then join, map

arr = [b"-", b"|", b"+"];
print(rb'[' + b''.join(map(bytes, list(map(lambda e: b'\\'+e, arr))))+b']+')
# b'[\\-\\|\\+]'

only here the question is:
what should your regular season do?
if you look at it rb"[\-|\||\+]+"
, apparently you want it to find a sequence of characters like -|+
something that does not need to be escaped in square brackets. in particular, only the hyphen "-" needs to be escaped, and then only if it is not at the beginning or end of the character sequence, [|\-+]
you can make a regular expression like this: [-|+]+
and it should work as it should

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question