简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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