[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-list
Subject:    Re: RE newbie question
From:       Rob Gaddi <rgaddi () highlandtechnology ! invalid>
Date:       2018-04-18 19:57:53
Message-ID: pb8801$48r$1 () dont-email ! me
[Download RAW message or body]

On 04/18/2018 12:37 PM, TUA wrote:
> import re
> 
> compval = 'A123456_8'
> regex = '[a-zA-Z]\w{0,7}'
> 
> if re.match(regex, compval):
> print('Yes')
> else:
> print('No')
> 
> 
> My intention is to implement a max. length of 8 for an input string. The above \
> works well in all other respects, but does allow for strings that are too long. 
> What is the proper way to fix this?
> 
> Thanks for any help!
> 

You could put the end marker $ on your regex so that it won't match if 
it's not the end.

Or, you know, you could just check len(compval) <= 8 and not get bogged 
down in regexes.  They tend to be excellent solutions to only a very 
specific complexity of problem.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.
-- 
https://mail.python.org/mailman/listinfo/python-list


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic