简体   繁体   English

如何匹配给定字符串中的特定行

[英]how to match particular lines from a given string

I want to get all the software versions from the below string.我想从以下字符串中获取所有软件版本。

a = """LXE:1#sho software
************************************************************************************
                Command Execution Time: Mon Mar 16 10:33:29 2020 UTC
************************************************************************************

====================================================================================================
                    software releases in /usr/rel/
====================================================================================================
VS00.8.1.5.0int014
VO00.8.1.5.0int012 (Back Rel)
V900.8.1.5.0int017 (Prim Rel)

---------------------------------------------------------------------------------------------------- Commit     : enabled
Commit Time  : 10 minutes
"""

I want the output as a list of all softwares as below:我希望输出为所有软件的列表,如下所示:

a = ['VS00.8.1.5.0int014','VO00.8.1.5.0int012 ','V900.8.1.5.0int017 ']

I have tried the below regex, but it is matching extra lines also.我试过下面的正则表达式,但它也匹配额外的行。

re.findall('[\w*\.\S*$\b]+',a)
['LXE:1#sho', 'software',
'************************************************************************************',
'Command', 'Execution', 'Time:', 'Mon', 'Mar', '16', '10:33:29', '2020', 'UTC',
'************************************************************************************',
'====================================================================================================',
'software', 'releases', 'in', '/usr/rel/',
'====================================================================================================',
'VS00.8.1.5.0int014', 'VO00.8.1.5.0int012', '(Back', 'Rel)', 'V900.8.1.5.0int017', '(Prim', 'Rel)',
'----------------------------------------------------------------------------------------------------',
'Commit', ':', 'enabled', 'Commit', 'Time', ':', '10', 'minutes']

How can I modify my Regex to just match what I need?如何修改我的正则表达式以匹配我需要的内容?

Your regex is confusingly wrong - I'm genuinely unsure what you were attempting to achieve.您的正则表达式错得令人困惑-我真的不确定您要实现的目标。 You can use a site like https://regex101.com/ to help you build and test regexes - it allows you to look at a reference and build the regex dynamically to make sure it does what you expect.您可以使用像https://regex101.com/这样的网站来帮助您构建和测试正则表达式 - 它允许您查看参考并动态构建正则表达式以确保它符合您的预期。

In regards to your requirements, you could match it with a regex like below - if you know how long certain sections will be and what characters they will be, you could make this substantially more efficient, however.关于您的要求,您可以将它与如下正则表达式匹配 - 如果您知道某些部分的长度以及它们将是什么字符,那么您可以大大提高效率。

re.findall(r"^V\S+\.\d+\.\d+\.\d+\.\S+", a)

A breakdown, and proof of functionality, can be found at https://regex101.com/r/twCQQN/1可以在https://regex101.com/r/twCQQN/1上找到细分和功能证明

暂无
暂无

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

相关问题 如何从python Regex中的给定字符串中提取特定长度的字符 - How to extract characters of particular length from a given string in python Regex Python-如何匹配和替换给定字符串中的单词? - Python - How to match and replace words from a given string? 如何通过使用python匹配特定的字符串 - how to match particular string by using python 如何根据特定的字符串匹配将特定字符串附加到另一个字符串? - How to append particular string to another string based on a specific string match? 如何从给定的json获取特定值 - How to get a particular value from the given json 如何在给定特定条件的情况下从文本字符串中选择行,并将其添加到列表中 - How can I select lines from a text string given certain criteria in python and add them to a list 从python Regex中的给定字符串中提取特定长度的字符 - Extraction of characters of particular length from a given string in python Regex 如果列表中给出了行,如何从文件中打印行? - How to print lines from file if lines are given in list? 如果在特定符号上匹配,如何将数据框中的字符串搜索并提取到新列中? - How to Search and Extract string in dataframe into new column if match on particular symbol? 如何从我的代码根据包含特定字符串选择的文本文件的行打印元素? - How can I print elements from lines of a text file that my code selects based on its inclusion of a particular string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM