[英]How to remove alphabets and extract numbers using regex in python?
How to remove alphabets and extract numbers using regex in python? 如何在python中使用正则表达式删除字母并提取数字?
import re
l=["098765432123 M","123456789012"]
s = re.findall(r"(?<!\d)\d{12}", l)
print(s)
Expected Output: 预期产出:
123456789012 123456789012
If all you want is to have filtered list, consisting elements with pure digits, use filter
with str.isdigit
: 如果您想要的是具有过滤列表,包含纯数字的元素,请使用带有str.isdigit
filter
:
list(filter(str.isdigit, l))
Or as @tobias_k suggested, list
comprehension is always your friend: 或者@tobias_k建议, list
理解总是你的朋友:
[s for s in l if s.isdigit()]
Output: 输出:
['123456789012']
To keep only digits you can do re.findall('\\d',s)
, but you'll get a list: 要只保留数字你可以re.findall('\\d',s)
,但你会得到一个列表:
s = re.findall('\d', "098765432123 M")
print(s)
> ['0', '9', '8', '7', '6', '5', '4', '3', '2', '1', '2', '3']
I would suggest to use a negative lookahead assertion, if as stated you want to use regex only. 我建议使用否定前瞻断言,如果声明你只想使用正则表达式。
l=["098765432123 M","123456789012"]
res=[]
for a in l:
s = re.search(r"(?<!\d)\d{12}(?! [a-zA-Z])", a)
if s is not None:
res.append(s.group(0))
The result would then be: 结果将是:
['123456789012']
So to be clear, you want to ignore the whole string if there is a alphabetic character in it? 所以要清楚,如果有一个字母字符,你想忽略整个字符串? Or do you still want to extract the numbers of a string with both numbers and alphabetic characters in it? 或者您是否仍想提取包含数字和字母字符的字符串的数字?
If you want to find all numbers, and always find the longest number use this: 如果你想找到所有数字,并且总是找到最长的数字,请使用:
regex = r"\d+"
matches = re.finditer(regex, test_str, re.MULTILINE)
\\d
will search for digits, +
will find one or more of the defined characters, and will always find the longest consecutive line of these characters. \\d
将搜索数字, +
将找到一个或多个已定义的字符,并始终找到这些字符的最长连续行。
If you only want to find strings without alphabets: 如果您只想查找没有字母的字符串:
import re
regex = r"[a-zA-Z]"
test_str = ("098765432123 M", "123456789012")
for x in test_str:
if not re.search(regex, x):
print(x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.