简体   繁体   English

如何在python中使用正则表达式删除字母并提取数字?

[英]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.

相关问题 使用Python正则表达式使用连字符提取电话号码 - Extract phone numbers with hyphen using Python regex 如何使用来自不同国家的各种格式的正则表达式在 Python 中提取电话号码(仅限数字)? - How to extract phone numbers (numbers only) in Python using Regex from various formats from different countries? Python正则表达式匹配包含字母和数字的8个字符的字符串 - Python regex match string of 8 characters that contain both alphabets and numbers 如何使用熊猫只保留数字和字母? - How to just keep numbers and alphabets using pandas? Python 正则表达式如何在字母和数字之间插入连字符; 并删除两个字母之间的连字符 - Python regex how to insert hyphen symbol between an alphabet and a digit; and also remove hyphen in between two alphabets Python3和正则表达式:如何删除数字行? - Python3 and regex: how to remove lines of numbers? 如何从python中的列表中删除英文字母 - How to remove english alphabets from list in python 我想从 python 3 中的字符串中提取所有十进制数,如何在不使用正则表达式的情况下做到这一点? - I want to extract all decimal numbers from a string in python 3, how can I do that without using regex? 使用python 2.7 regex从电子邮件中提取电话号码 - Extract phone numbers from email using python 2.7 regex 如何在python中使用正则表达式从字符串中提取小数点数字和百分比 - How to extract decimal point numbers and percentages from a string using regex in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM