I’m very new with the syntax of regex, I already read some about the libary. I’m trying extract names from a simple sentence, but I found myself in trouble, below I show a exemple of what I’ve done.
JavaScript
x
6
1
x = 'Fred used to play with his brother, Billy, both are 10 and their parents Jude and Edde have two more kids.'
2
3
import re
4
5
re.findall('^[A-Za-z ]+$',x)
6
Anyone can explain me what is wrong and how to proceed?
Advertisement
Answer
I think your regex has two problems.
- You want to extract names of sentence. You need to remove
^
start of line and$
end of line. - Name starts with uppercase and does not have space. You should remove
You could use following regex.
JavaScript
1
2
1
b[A-Z][A-Za-z]+b
2
I also tried to test result on python.
JavaScript
1
7
1
x = 'Fred used to play with his brother, Billy, both are 10 and their parents Jude and Edde have two more kids.'
2
3
import re
4
5
result = re.findall('\b[A-Z][A-Za-z]+\b',x)
6
print(result)
7
Result.
JavaScript
1
2
1
['Fred', 'Billy', 'Jude', 'Edde']
2