Reputation: 1159
Hello I'd like to extract the content of this tag
<Sentiment int=6>Deep injustice</Sentiment>
in many sentences of text (Here).
df['text'].str.extractall(r'^<(?P<Sentiments>\w+).*[int]?.*(?P<Intensite>\d?\d)>(?P<Expression>[a-zA-Z]*?.*[a-zA-Z]*)<')
My code produce only few of them(tag). Why it do not extract others?
Sentiments Intensite Expression
match
405 0 Disagreement 3 Bizarre contradiction
921 0 Satisfaction 5 La plus simple
2549 0 Dissatisfaction 3 Ne me contentant pas
Upvotes: 0
Views: 115
Reputation: 626758
You may use
df['text'].str.extractall(r'<(?P<Sentiments>\w+)\s+int=(?P<Intensite>\d+)>(?P<Expression>[^<]*)')
See the regex demo.
Details
<
- a <
char(?P<Sentiments>\w+)
- Group "Sentiments": 1 or more letters, digits, underscores\s+
- 1+ whitespaceint=
- a substring (?P<Intensite>\d+)
- Group "Intensite": 1+ digits>
- a >
char(?P<Expression>[^<]*)
- Group "Expression": 0 or more chars other than >
Upvotes: 1