简体   繁体   English

在长字符串中的符号之间提取特定数字

[英]Extract Specific Number in between symbols in a long string

I have a long string captured from a log txt file which have sequence as below:我从日志 txt 文件中捕获了一个长字符串,其序列如下:

526136|20190403164654| 526136|20190403164654| 3 |06010003530075508541|1|8801851088890| 3 |06010003530075508541|1|8801851088890|

Is there any method to capture the 3rd field, which is the "3" value from each line using regex?有什么方法可以捕获第三个字段,即使用正则表达式从每行中获取的“3”值? I am aware that i can use the findall function to search for all number values and then capture the 3rd element in the list.我知道我可以使用 findall function 来搜索所有数值,然后捕获列表中的第三个元素。 Is there any other methods that is more efficient and simpler in coding?有没有其他更高效、更简单的编码方法? Thanks all谢谢大家

You don't need a regex for this, you can just .split() and find the Nth element!您不需要正则表达式,您只需.split()并找到第 N 个元素!

>>> s = "526136|20190403164654|3|06010003530075508541|1|8801851088890|"
>>> s.split("|")[2]
'3'
import re
text = "526136|20190403164654|3|06010003530075508541|1|8801851088890"

pattern = r"\|\d\|"
new_text = re.search(pattern,text)
print(new_text)

#or

new_text = text.split("|")
print(new_text[2])

Try this:尝试这个:

>>> a = """1|2|3|4|5|
... 6|7|8|9|10|
... 11|12|13|14|15|
... 16|17|18|19|20|"""
>>> re.findall(r'^.*?\|.*?\|([\d]*?)\|', a, 10)
['3', '8', '13', '18']
import re ptrn = re.compile(r"\|") s = "526136|20190403164654|3|06010003530075508541|1|8801851088890|" number = re.split(ptrn, s)[2]

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

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