Issue with re.findall - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Issue with re.findall (/thread-13504.html) |
Issue with re.findall - alinaveed786 - Oct-18-2018 I m stuck with re.findall . Basically, below code, I m trying to find whether certain patches as defined by patches list is applied to the Oracle home or not. #!/usr/bin/python import re import subprocess s = subprocess.check_output(["opatch", "lsinventory"]) output = s.decode("utf-8") patches = [ 27923320,27547329,21463894] searchObj = re.findall(r patches[1:], output, re.M|re.I) if searchObj: print ( searchObj.group(),"detected") else: print ("Nothing found!!")below the error
RE: Issue with re.findall - buran - Oct-18-2018 Can you show us what you get as an output from subprocess.check_output()? Probably [with some transformation of the output] you can use set operations and not RegEx. The error is because r patches[1:] is not valid code - that's clear
RE: Issue with re.findall - alinaveed786 - Oct-18-2018 Yes, it's the syntax error. Any other way to pass list values as patterns to re.search or re.findall ? RE: Issue with re.findall - buran - Oct-18-2018 you need to use RegEx. If you are not familiar with regex expression syntax read https://docs.python.org/3/library/re.html#regular-expression-syntax and also https://www.regular-expressions.info/ you can use http://regex101.com to test your regex expression RE: Issue with re.findall - alinaveed786 - Oct-18-2018 I was able to figure out the correct usage of re.findall but I want each list value in the new line. Can we use for loop to pass list value to re.findall and then print each output in a new line ? #!/usr/bin/python import os import re import subprocess s = subprocess.check_output(["opatch", "lsinventory"]) output = s.decode("utf-8") patches = [27923320, 27547329, 21463894] searchObj = re.findall(str(patches), output, re.M|re.I) if searchObj: print('Patch', patches, "detected") else: print("Nothing found!!")The output Patch [27923320, 27547329, 21463894] detected RE: Issue with re.findall - ichabod801 - Oct-18-2018 You have a list of numbers. Regexes are for strings, not lists and not numbers. Do you want to test the regex on the string version of the numbers??? RE: Issue with re.findall - buran - Oct-18-2018 (Oct-18-2018, 01:07 PM)ichabod801 Wrote: Do you want to test the regex on the string version of the numbers???I think OP want to create RegEx pattern(s) from this list of numbers and test the output they get from subprocess.check_output()
RE: Issue with re.findall - alinaveed786 - Oct-20-2018 I was able to achieve via below modifications. Thanks everyone for all the help and support #!/usr/bin/python import re import subprocess s = subprocess.check_output(["opatch", "lsinventory"]) output = s.decode("utf-8") patches = [27923320, 27547329, 21463894, 12345, 99999] patches_found = set(re.findall(r'\b(?:%s)\b' % '|'.join(map(str, patches)), output)) patches_missing = set(map(str, patches)) - patches_found if patches_found: print('Patch', patches_found, "detected") if patches_missing: print("Patch", patches_missing, "missing") RE: Issue with re.findall - volcano63 - Oct-20-2018 (Oct-20-2018, 05:57 AM)alinaveed786 Wrote: I was able to achieve via below modifications. Thanks everyone for all the help and support You convert patches elements to strings twice - would not it have been easier to just define them as strings?!
|