Home  >  Article  >  Backend Development  >  How to solve the error 'nothing to repeat at position 0' when Python uses regular expressions

How to solve the error 'nothing to repeat at position 0' when Python uses regular expressions

WBOY
WBOYforward
2023-04-28 14:28:061729browse

In the process of using regular expressions in python, the problem of [nothing to repeat at position 0] often occurs when handwriting filtering content. It is usually due to the problem of unrecognized symbols. Let’s take a look at the error examples:

info = r" |-|*|/|×|÷|x|X"

You can see this exception:

How to solve the error nothing to repeat at position 0 when Python uses regular expressions

The above is a regular symbol filter, in which the plus sign [ ] and multiplication sign [*] are within the basic symbols of regular expressions and are equivalent to keywords. We As we all know, keywords cannot be used. We only need to put a layer of [[]] square brackets outside the keywords.

Let’s try to solve it:

You can see that it was successful

How to solve the error nothing to repeat at position 0 when Python uses regular expressions

In fact, I just used the plus sign【 】After doing experiments, in fact, there are many symbols in regular expressions. When we need to obtain these symbols, we need to follow the above method.

So, let me list the symbols here, just pay attention when using them:

##"*": Matches the previous subexpression zero times or multiple times.

" ": Match the previous subexpression one or more times.
"?": Match the previous subexpression zero or one time.
"()": Marks the beginning and end of a subexpression.
".": Matches any single character except the newline character \n
"[": Marks the beginning of a bracket expression
"\": Marks the next character as a special character , or a literal character, or a backward reference, or an octal escape character.
"^": Matches the beginning of the input string, unless used in a square bracket expression, in which case it indicates that the character set is not accepted
"{": Marks the beginning of the qualifier expression.
"|": Specifies a choice between two items.

These are just symbols, there are some special ones, such as \d, which actually need to be processed using square brackets.

The above is the detailed content of How to solve the error 'nothing to repeat at position 0' when Python uses regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete