-1

I am trying to match a simple pattern using re with python but I can't manage to reach a solution.

So I have to match a pattern like:

word.word

but not for example

word.word.word

My current REGEX is [A-Za-z]+\.[A-Za-z]+ and matches both (wrong), I tried also with ^$ to include start and end of string but does not match any of them. I am doing something wrong, I really appreciate some help. Thanks

ldg
  • 450
  • 3
  • 7
  • 27

2 Answers2

0
re.match('^[A-Za-z]+\.[A-Za-z]+$', 'word.word') # return result
re.match('^[A-Za-z]+\.[A-Za-z]+$', 'word.word.word') # return None
Ashutosh gupta
  • 447
  • 4
  • 16
0

To match 2-words sequences (joined with . and containing only letters) within a text:

(also considering that a sequence could be followed with . pointing to the end of the sentence)

import re

test_str = 'some text word.word2.word3 with word.word and text with 1www.wewe11 and wordA.wordB.'
words = re.findall(r'(?<!\.)[a-z]+\.[a-z]+\b(?=[^\.]|\.?$)', test_str, re.I)

print(words)

The output:

['word.word', 'wordA.wordB']
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105