Question

i need some help with a regular expression that i use in some Python code, and i have almost created the expression that i need, i am very close. Here are the Python code that i use:

import re

def main():

    f = open('/tmp/file', 'r')
    rexp = re.compile('(?m)^   [*] ''([^ ]+).*\(([^ \)]+)')
    upgrades = rexp.findall(f.read())

    print upgrades

    f.close()

main()

And this is the content of /tmp/file:

Software Update Tool
Copyright 2002-2010 Apple

2014-03-18 14:31:28.958 softwareupdate[5505:3603] No alternate URLs found for packageId     MobileDevice
Software Update found the following new or updated software:
   * SecUpd2014-001-1.0
        Security Update 2014-001 (1.0), 112751K [recommended] [restart]
   * Safari6.1.2MountainLion-6.1.2
        Safari (6.1.2), 51679K [recommended]
   * iTunesXPatch-11.1.5
        iTunes (11.1.5), 79522K [recommended]

With the expression above i get the following output:

[('SecUpd2014-001-1.0\n', '1.0'), ('Safari6.1.2MountainLion-6.1.2\n', '6.1.2'), ('iTunesXPatch-11.1.5\n', '11.1.5')]

For my question, how can i change my expression so the output becomes like this?

[('SecUpd2014-001-1.0', '1.0'), ('Safari6.1.2MountainLion-6.1.2', '6.1.2'), ('iTunesXPatch-11.1.5', '11.1.5')]

I have been searching around for similar scenarios, but regexs tend to be very specific so i was unable to find anything helpful. If you need more info just ask, i would appreciate any help i can get.

Was it helpful?

Solution

Use [\n\r] in your regex to place it outside the capture will do the trick for you.

rexp = re.compile('(?m)^   [*] ''([^ ]+)[\n\r].*\(([^ \)]+)')
                                        ^^^^^^
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top