D
D
DTPlayer2021-09-20 21:28:58
Python
DTPlayer, 2021-09-20 21:28:58

How to set range and characters in regular expression?

import re
data = "DaTa123"
print(re.search("\d[0-9A-Za-z]{1, 5}", data))

How to run such a script?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-09-20
@DTPlayer

First, \something is a way of specifying a character in a string.
In regular expressions, this syntax has its own meaning.
As a consequence of this conflict, one of two things must be done.

re.search("\\d[0-9A-Za-z]{1,5}", data) #удваиваем \
re.search(r"\d[0-9A-Za-z]{1,5}", data) #используем r-строку, чтобы отключить обычную обработку \

Well, yes, "{1,5}" should not contain a space.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question