给出类似的文字
A 0 1 2 3 4
我想将每个数字的A匹配为单独的匹配,例如
re.findall(some_regex, "A 0 1 2 3 4")
会返回,
[
["A", "0"],
["A", "1"],
["A", "2"],
["A", "3"],
["A", "4"],
]
答案 0 :(得分:1)
dd ='A01234'
Result = [[dd[0],j] for i,j in enumerate(dd) if i!=0]
#Output = [['A', '0'], ['A', '1'], ['A', '2'], ['A', '3'], ['A', '4']]
答案 1 :(得分:0)
那是不可能的。但是您可以轻松生成这样的列表。
x = re.findall("A*[0-9]", "A 0 1 2 3 4")
result = [["A", str(c)] for c in x]
答案 2 :(得分:0)
尝试一下,我用两个Python regex
完成了。
type gallery
输出为
import re
text = "A 0 1 2 3 4" # your text here
"""
select first character which belongs to alphabetically
between a and z, lower case OR upper case. If you need
to match only upper case character, just change pattern1
into "^[A-Z]"
pattern2 will match all the string contains with digits
which mean numbers from 0-9
"""
pattern1 = "^[A-Za-z]"
pattern2 = "\d"
print([[re.search(pattern1, text).group(0), a] for a in re.findall(pattern2, text)])
现在,您可以根据需要更改任意数量的第一个字符。