简体   繁体   中英

Regex in Python: “\d+” and "[0-9]+ do NOT only recognize numbers&Digits but also Operators?

in all referencebooks and forum discussions covering this point I always read that the following expressions "[0-9]" and "\\d" only recognize single digits (and nothing else)

Therefore I would conclude that the regular expression "\\d+" would only recognize strings made up exclusively of digits and nothing else (eg "343535" ).

However, this expression also recognizes strings that could represent mathematical operations.

For example: checking the strings "3-5" or "5+6" for the regex "\\d+" will always return True

How can that be? And how do I have to modify my regex if I want to make sure that strings really ONLY contain digits (and no Operators)?

You need to anchor your regular expressions:

r'^\d+$'

otherwise you'll match part of the string; any string with 1 or more digits will match unless you tell the parser to also look for the start and end of the input string.

Demo:

>>> import re
>>> a_number = re.compile(r'\d+')
>>> only_a_number = re.compile(r'^\d+$')
>>> a_number.search('This string is not a number, but does *contain* 1') is not None
True
>>> only_a_number.search('This string is not a number, but does *contain* 1') is not None
False
>>> a_number.search('1') is not None
True
>>> only_a_number.search('1') is not None
True

Note that I am using re.search() here; if you use re.match() instead an implicit \\A is added to the start of the pattern (match at the beginning of the input string).

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