Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking whole string with a regex

Tags:

I'm trying to check if a string is a number, so the regex "\d+" seemed good. However that regex also fits "78.46.92.168:8000" for some reason, which I do not want, a little bit of code:

class Foo():     _rex = re.compile("\d+")     def bar(self, string):          m = _rex.match(string)          if m != None:              doStuff() 

And doStuff() is called when the ip adress is entered. I'm kind of confused, how does "." or ":" match "\d"?

like image 484
dutt Avatar asked Oct 22 '10 06:10

dutt


People also ask

What is full match in regex?

The fullmatch() function returns a Match object if the whole string matches the search pattern of a regular expression, or None otherwise. The syntax of the fullmatch() function is as follows: re.fullmatch(pattern, string, flags=0)

What is test string in regex?

test() method is used to test for a match in a string. The method returns true if it finds a match; otherwise, it returns false .

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).


2 Answers

\d+ matches any positive number of digits within your string, so it matches the first 78 and succeeds.

Use ^\d+$.

Or, even better: "78.46.92.168:8000".isdigit()

like image 197
eumiro Avatar answered Oct 02 '22 02:10

eumiro


There are a couple of options in Python to match an entire input with a regex.

Python 2 and 3

In Python 2 and 3, you may use

re.match(r'\d+$') # re.match anchors the match at the start of the string, so $ is what remains to add 

or - to avoid matching before the final \n in the string:

re.match(r'\d+\Z') # \Z will only match at the very end of the string 

Or the same as above with re.search method requiring the use of ^ / \A start-of-string anchor as it does not anchor the match at the start of the string:

re.search(r'^\d+$') re.search(r'\A\d+\Z') 

Note that \A is an unambiguous string start anchor, its behavior cannot be redefined with any modifiers (re.M / re.MULTILINE can only redefine the ^ and $ behavior).

Python 3

All those cases described in the above section and one more useful method, re.fullmatch (also present in the PyPi regex module):

If the whole string matches the regular expression pattern, return a corresponding match object. Return None if the string does not match the pattern; note that this is different from a zero-length match.

So, after you compile the regex, just use the appropriate method:

_rex = re.compile("\d+") if _rex.fullmatch(s):     doStuff() 
like image 20
Wiktor Stribiżew Avatar answered Oct 02 '22 02:10

Wiktor Stribiżew