简体   繁体   English

使用python正则表达式查找特定字符

[英]Find specific character with python regex

I have a list of strings looking like this: 我有一个字符串列表,如下所示:

H PL->01 Tx=000/006 Ph=00/000  DGDD DDDR YDyD GRDD YGR  Dets=     003,003,003,003,003,003,003,003,003,003,003,003,  ports= 255,255,255,255,255,255,255,255,'

I want to be able to extract the content tha matches DGDD DDDR YDyD GRDD YGR (this changes but always has the letters D,G,R,Y,y and its length may change) and put it in a list without whitespaces like this: 我希望能够提取匹配DGDD DDDR YDyD GRDD YGR (这会改变,但总是有字母D,G,R,Y,y并且它的长度可能会改变)并将其放在没有像这样的空格的列表中:

 ['D', 'G', 'D', 'D', 'D', 'D', 'D', 'R', 'Y', 'D', 'y', 'D', 'G', 'R', 'D', 'D', 'Y', 'G', 'R']

If the criteria is groups of DGRYy that have at least three characters, then you can use a regex to that effect and then "flatten" it to a list after... eg: 如果条件是具有至少三个字符的DGRYy组,那么您可以使用正则表达式来实现该效果,然后将其“展平”到列表后...例如:

import re
from itertools import chain
print list(chain.from_iterable(re.findall('[DGRYy]{3,}', data)))
# ['D', 'G', 'D', 'D', 'D', 'D', 'D', 'R', 'Y', 'D', 'y', 'D', 'G', 'R', 'D', 'D', 'Y', 'G', 'R']

If it's always between two items, then it's possible to use the builtin string functions to extract it, eg: 如果它总是在两个项目之间,则可以使用内置字符串函数来提取它,例如:

print [ch for ch in data[data.index('Ph'):].partition('Dets=')[0].split(' ', 1)[1] if ch != ' ']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM