simplycoding
simplycoding

Reputation: 2977

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

Upvotes: 1

Views: 114

Answers (2)

Shashank
Shashank

Reputation: 13869

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

Upvotes: 0

idelcano
idelcano

Reputation: 123

 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).

Upvotes: 1

Related Questions