So, I’ve searched for a while and couldn’t find anything, so I’ve decided to turn to the experts on SO to help me clarify what is going on.
I am learning Python and whilst learning about regular expressions, I’ve come across an interesting bit of syntax that I can’t quite figure out. In this example, a function is defined which runs a regular expression on the input parameter, returning the match of the integer as a float, or throwing an exception if the input doesn’t match something that looks like a number:
import re
def getNumber(token):
r'-?[1-9][0-9]*.?[0-9]*'
return float(token)
this function can be called like so:
print getNumber('123.123')
print getNumber('123.123')+40
which would output:
123.123
163.123
I am trying to understand the mechanics of how this is happening. I understand that we are declaring a regular expression object with the call to r’STRING’, but somehow just declaring that regular expression causes the token parameter to get passed into the expression as well. Is this a trait with functions explicitly containing a parameter called “token”? Is there a behavior associated with multiple parameters? It definitely seems that some work has been done here to provide a pythonic syntax, I would just like to know the details of how it works and how to use it in the future. Pointing me to documentation would be great, as I couldn’t find anything on the subject.
This function isn’t running a regular expression on the input … It’s calling
floaton the input which is converting the input to a floating point number. The “regular expression” that is there is nothing more than a docstring on the function.Note that
r"this is a string"simply creates a “raw string” which has nothing to do with regex ("\t"is tab whereasr"\t"is the literal characters'\'and't') .