简体   繁体   中英

Python Regex expression

Trying to write a Regex expression in Python to match strings.

I want to match input that starts as first , first?21313 but not first.

So basically, I don't want to match to anything that has . the period character.

I've tried word.startswith(('first[^.]?+')) but that doesn't work. I've also tried word.startswith(('first.?+')) but that hasn't worked either. Pretty stumped here

 import re  
 def check(word):
        regexp = re.compile('^first([^\..])+$')
        return regexp.match(word)

And if you dont want the dot: ^first([^..])+$ (first + allcharacter except dot and first cant be alone).

You really don't need regex for this at all.

word.startswith('first') and word.find('.') == -1

But if you really want to take the regex route:

>>> import re
>>> re.match(r'first[^.]*$', 'first')
<_sre.SRE_Match object; span=(0, 5), match='first'>
>>> re.match(r'first[^.]*$', 'first.') is None
True

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