Reputation: 595
I'd like that only one-digit number to be picked.
On windows python
, using Japanese unicode
, I've:
s = "17 1 27歳女1"
re.findall(r'[1-7]\b', s)
I need to match the second 1
and last 1
in s
- not 17
initial 1
.
Desired output:
['1', '1']
Upvotes: 2
Views: 516
Reputation: 5958
This is the regex you're looking for:
(?<!\d)[1-7](?!\d)
Test:
import re
s="17 1 27歳女1"
re.findall(r'(?<!\d)[1-7](?!\d)', s)
Output:
['1', '1']
Upvotes: 2
Reputation: 98881
Try using a negative-lookbehind (?<!\d)
. This will ignore matches where a digit is preceded by another one, i.e.:
import re
s = "17 1 27歳女1"
x = re.findall(r"(?<!\d)[1-7]\b", s)
print(x)
# ['1', '1']
Regex Explanation:
Upvotes: 3