I was trying to match text with regular expression.
The following code works a bit strangely. It returns the result twice.
regex = r"((\w+\s*){1,3} from)"
test_str = "text text this is Alex Smith from text text"
re.findall(regex, test_str)
Can someone point whats wrong here?
If you are curious, my final goal is to mach any 2/3 words NAMEs like 'Alex Smith or Mr. Alex Smith' between (or at one side) specific text. For instance,
1. this is Alex Smith from Japan (2/3 words after 'this is' or before from)
2. this is Mr. Alex Smith from japan (Optional Mr.)
2. Mr. Alex Smith from Tokyo (2/3 words before from)
3. this is Alex Smith text text
So basically it should trigger on 'this is' or 'from'. Any suggestion? text text Alex Smith from Japan I am Alex Smith text text