简体   繁体   English

使用 RegEx 在文件中搜索以使用 Python 查找 ID 及其编号

[英]Searching in file using RegEx to find ID and its number with Python

I am fairly new to regEx and I am completely baffled by it at this point so any help will be greatly appreciated.我对 regEx 还很陌生,此时我完全被它弄糊涂了,所以任何帮助都将不胜感激。 Sorry if this question is around already, could not find it.抱歉,如果这个问题已经存在,找不到它。 All of this is happening in Python所有这一切都发生在 Python

I am trying to do a search inside text file which has some text like:我正在尝试在包含如下文本的文本文件中进行搜索:

www.google.com, something, something: something, [{'id': 481, 'name': 'name it needs to match'}]

="1000" t5:someplace="7713" t5:somethingelse="10" t5:someotherthing="10"

it have multiple, very long lines (its a log, essentially).它有多个非常长的行(本质上是一个日志)。

What I need to do is to find a word "id" with its number by the "name" that it have inside the brackets and keep only the number (or all of it, doesn't matter) so I can put it back into the program (I am trying to create something that already exists trough API and I want to use Id of the existing thing to use it later on)我需要做的是通过括号内的“名称”找到一个带有数字的单词“id”,并只保留数字(或全部,没关系),这样我就可以把它放回程序(我正在尝试通过 API 创建已经存在的东西,我想使用现有东西的 ID 稍后使用它)

I am pretty sure I can do the slicing after I have filtered ID with its number but I am struggling to write RegEx that can find "Id" and its number by NAME.我很确定在我用它的编号过滤 ID 后我可以进行切片,但是我正在努力编写可以通过 NAME 找到“Id”及其编号的 RegEx。

Anyone have some insight?有人有一些见识吗? any advice let alone piece of code would be an immense help.任何建议,更不用说一段代码,都会有很大的帮助。 I concur that it is completely possible there is far easier solution that I am not seeing and I will be grateful if anyone can tell me said solution, I am here to learn as much as possible:)我同意完全有可能有我没有看到的更简单的解决方案,如果有人能告诉我所说的解决方案,我将不胜感激,我在这里尽可能多地学习:)

PS: position of the "name" variable is almost always the same or at least very close to it, is there a way to use RegEx to say find the "name" and then slice everything around the "name" for approx 15-20 characters? PS:“名称”变量的 position 几乎总是相同或至少非常接近它,有没有办法使用正则表达式来查找“名称”,然后将“名称”周围的所有内容切片大约 15-20人物? I could then filter out the number only since I will know it is there.然后我可以过滤掉这个数字,因为我会知道它在那里。

Try尝试

'id': (\d+),.*?'name': '([^']*)'

This way you'll get the ID in capture group 1 and the name in capture group 2.这样,您将获得捕获组 1 中的 ID 和捕获组 2 中的名称。

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

相关问题 正则表达式使用Python正则表达式查找特定数字 - Regex to find specific number using Python regex 使用正则表达式搜索字符串列表以查找子字符串Python - Searching List of Strings Using Regex to Find Substrings Python 使用带有python的正则表达式来查找唯一的数字格式 - Using regex with python to find unique number format 在 Python 中使用正则表达式在 CSV 文件中搜索特定短语 - Searching for a specific phrase in CSV file using regex in Python 如何在 PYTHON 中打开文件并使用正则表达式修改其内容以查找/匹配/替换模式 - How to open a file in PYTHON and modify its contents using regex to find/match/substitute patterns 使用 Python 在文本文件中搜索重复模式的数量 - Searching for number of repeated patterns in text file using Python 尝试使用正则表达式替换文本文件中的数字以在Python中找到正确的行 - Trying to replace a number in text file using regex to find the right line in Python 使用python,regex和glob查找最新文件 - Find the latest file using python, regex and glob 使用Python Regex在File中查找C函数 - Using Python Regex to Find C function in File Python中有没有办法找到名称中数字最小的文件? - Is there a way in Python to find a file with the smallest number in its name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM