Findall pattern usind python re

Viewed 47

Thanks in advance
I want to find a certain pattern in re. I tried different combinations and this is where i am at.
Here is the code:

import re

txt = "Suggested Profile(s) : Win7SP1x64, Win7SP0x64, Win2008R2SP1x64, Win7SP1x64_24000, Win2008R2x32, Win7x32"

match = re.findall(r"Win.+64", txt)

if match:
    y = match[0]

try:
    profiles = y.split(", ")

    for profile in profiles:
        print(profile)
except Exception:
    print("Error")

When I run this, I get the following output:

Win7SP1x64
Win7SP0x64
Win2008R2SP1x64
Win7SP1x64

which is somewhat correct, but it is not showing the complete output. The last word "Win7SP1x64" is not complete, it is "Win7SP1x64_24000" but in output, it only shows till '64'.
Also, i want script to show the words with have '32' as well, not sure how to implement this.

2 Answers

I think you are looking for some expression like r"Win[^,]+x[2346][_\d]*"

txt = "Suggested Profile(s) : Win7SP1x64, Win7SP0x64, Win2008R2SP1x64, Win7SP1x64_24000, Win2008R2x32, Win7x32"

match = re.findall(r"Win[^,]+x[2346][_\d]*", txt)

The match variable will contain:

['Win7SP1x64', 'Win7SP0x64', 'Win2008R2SP1x64', 'Win7SP1x64_24000', 'Win2008R2x32', 'Win7x32']

You need to match the trailing characters after 64 too. The regex you specified is only until 64

From your solution, you can do it like so:

match_64 = re.findall(r"Win\w+64\w*", txt)

and for 32

match_32 = re.findall(r"Win\w+x32\w*", txt)
Related