简体   繁体   English

正则表达式模式和python中的列表

[英]regex patterns and lists in python

I have hunted around various other posts and although there are some useful tips I haven't found a similar problem to mine so I thought I would ask. 我搜寻了许多其他帖子,尽管有一些有用的提示,但我没有发现类似的问题,因此我想问一下。 I have generated the following list: 我生成了以下列表:

data2 = ['AN1_OUT,24','AN2_OUT,13','AN3_OUT,14','AN4_OUT,15']

What I want to do is identify the setting (AN1_OUT etc..) and the value (2,13 etc...) that accompanies it. 我要确定的是设置(AN1_OUT等)和相应的值(2,13等)。 I have successfully identified the setting by using the good old 'if-elif' as I only need to know this setting, however, I now need to separate out the value. 我已经通过使用旧的'if-elif'成功识别了该设置,因为我只需要知道此设置,但是,现在我需要分离出该值。 So far I am using: 到目前为止,我正在使用:

data3 = re.findall('[0-9]{2}',data2[i])
byte1 = map(lambda n: int(n[:2]),data3)

This is in a for loop that runs through all of the elements in the data2 list (4 in this example). 在for循环中,该循环遍历data2列表中的所有元素(此示例中为4)。 for each 'i' I am getting the following: 对于每个“ i”,我得到以下信息:

[24]
[13]
[14]
[15]

I know this is what I would expect, however, the problem arises when the value is a single digit such as: 我知道这是我所期望的,但是,当值是个数字时,就会出现问题,例如:

'AN1_OUT,2' 

In this case I miss that value and it is not printed. 在这种情况下,我会错过该值,并且不会打印出来。 I tried changing the regex in the data3 function to: 我尝试将data3函数中的正则表达式更改为:

data3 = re.findall('[0-9]{1,2}',data2[i])

However the problem with this is that it picks up the digit in AN1_OUT, AN2_OUT etc.. so I end up with: 但是,这样做的问题是它拾取了AN1_OUT,AN2_OUT等中的数字。所以我最终得到了:

[1,2]
[2,13]
[3,14]
[4,15]

I have looked at various different ways to solve it but it is proving very elusive. 我已经研究了解决该问题的各种不同方法,但是事实证明它非常难以捉摸。 Any help would be appreciated. 任何帮助,将不胜感激。

Append $ at the end to make it match only at the end of the input string: 在末尾附加$使其仅在输入字符串的末尾匹配:

'[0-9]{1,2}$'

You can use \\d instead of [0-9] : 您可以使用\\d代替[0-9]

'\\d{1,2}$'

To avoid escape use raw string ( r'raw string' ): 为了避免转义,请使用原始字符串( r'raw string' ):

r'\d{1,2}$'

>>> re.findall(r'\d{1,2}$', 'AN3_OUT,14')
['14']

>>> re.findall(r'\d+$', 'AN3_OUT,14')
['14']

You can use look-behind to fetch the digit preceded by comma . 您可以使用向后查找来获取逗号前面的数字。 Also, you can use [0-9]+ instead of [0-9]{1,2} , id you can have more digits. 另外,您可以使用[0-9]+代替[0-9]{1,2} ,您可以使用更多数字。

data3 = re.findall(r'(?<=,)[0-9]+',data2[i])

You can parse the strings you've described without using regular expressions. 您可以解析描述的字符串,而无需使用正则表达式。 Just split on the comma! 只是用逗号分开!

for item in data2:
    setting, value = item.split(',')
    if setting == 'AN1_OUT':
        value = int(value)
        # do stuff with value

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

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