简体   繁体   English

Python重新找到所有组合打印为字符串

[英]Python re find all combinations print as string

Using Python regular expressions how can I find all instances of a combination and print each one on a new line? 使用Python正则表达式,如何找到组合的所有实例并在新行上打印每个实例?

Example: 例:

import re

x = "a=123,b=123,c=123,d=123,a=456,b=456...etc"
y = re.search('a=(.*?),', x)
print(y)

Trying to get: 试图获得:

123
456

The regular expression 正则表达式

First of all, your regular expression is incorrect. 首先,您的正则表达式不正确。 You're matching a= followed by any number of characters. 您要匹配a=后跟任意数量的字符。 This will match the entire string in one go because * is mostly greedy. 这将一次匹配整个字符串,因为*大多是贪婪的。 Instead, you're trying to find any number of letters, an equal sign, and then any number of digits. 相反,您尝试查找任意数量的字母,等号以及任意数量的数字。

[A-Za-z]+=(\d+)  Regular Expression
        +        At least one
[A-Za-z]         (English) letter
         =       An equals sign
          (   )  Group 1
             +   At least one
           \d    digit

Also, use re.findall not re.search . 另外,请使用re.findall而不是re.search

Then, doing re.findall(r"[A-Za-z]+=(\\d+)", x) will give the list of strings, which you can print, parse, whatever. 然后,执行re.findall(r"[A-Za-z]+=(\\d+)", x)将给出字符串列表,您可以打印,解析任何内容。

Also, there might be a better way of doing this: if the data is exactly as you format it, you can just use regular string operations: 另外,可能会有更好的方法:如果数据完全符合您的格式,则可以使用常规的字符串操作:

a = "a=123,b=456,c=789"
b = a.split(",") # gets ["a=123", "b=456", "c=789"]
c = [E.split("=") for E in b] # gets [["a", "123"], ["b", "456"], ["c", "789"]]

Then, if you want to turn this into a dictionary, you can use dict(c) . 然后,如果要将其转换为字典,则可以使用dict(c) If you want to print the values, do for E in c: print(E[1]) . 如果要打印值,请for E in c: print(E[1]) Etc. 等等。

Just use re.findall : 只需使用re.findall

import re
x = "a=123,b=123,c=123,d=123,a=456,b=456...etc"
final_data = re.findall("(?<=a\=)\d+", x)
for i in final_data:
   print(i)

Output: 输出:

123
456

This regular expression utilizes a positive look behind to make sure that the digits are part of the a= expression: 此正则表达式利用积极的眼光来确保数字是a=表达式的一部分:

\\d+ : matches all digits until non-numeric character is found (in this case the start of the next expression). \\d+ :匹配所有数字,直到找到非数字字符(在这种情况下,下一个表达式的开始)。

(?<=a\\=) : searches for a= assignment part of expression and acts as anchor for \\d+ regex. (?<=a\\=) :搜索表达式的a=赋值部分,并充当\\d+正则表达式的锚点。

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

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