[Tutor] Regular expression error
Kent Johnson
kent37 at tds.net
Thu Jul 28 21:21:10 CEST 2005
Bernard Lebel wrote:
> Hello,
>
> I'm using regular expressions to check if arguments of a function are
> valid. When I run the code below, I get an error.
>
> Basically, what the regular expression "should" expect is either an
> integer, or an integer followed by a letter (string). I convert the
> tested argument to a string to make sure. Below is the code.
>
>
>
> # Create regular expression to match
> oPattern = re.compile( r"(\d+|\d+\D)", re.IGNORECASE )
This will match a string of digits followed by any non-digit, is that what you want? If you want to restrict it to digits followed by a letter you should use
r"(\d+|\d+[a-z])"
Also this will match something like 123A456B, if you want to disallow anything after the letter you need to match the end of the string:
r"(\d+|\d+[a-z])$"
>
> # Iterate provided arguments
> for oArg in aArgs:
>
> # Attempt to match the argument to the regular expression
> oMatch = re.match( str( oArg ), 0 )
The problem is you are calling the module (re) match, not the instance (oPattern) match. re.match() expects the second argument to be a string. Just use
oMatch = oPattern.match( str( oArg ), 0 )
The hint in the error is "expected string or buffer". So you are not giving the expected argument types which should send you to the docs to check...
Kent
>
>
>
> The error I get is this:
>
> #ERROR : Traceback (most recent call last):
> # File "<Script Block >", line 208, in BuildProjectPaths_Execute
> # aShotInfos = handleArguments( args )
> # File "<Script Block >", line 123, in handleArguments
> # oMatch = re.match( str( oArg ), 0 )
> # File "D:\Python24\Lib\sre.py", line 129, in match
> # return _compile(pattern, flags).match(string)
> #TypeError: expected string or buffer
> # - [line 122]
More information about the Tutor
mailing list