Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with re.findall
#1
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
Error:
/u02/scripts/Patching/venv/bin/python /u02/scripts/Patching/Patch.py File "/u02/scripts/Patching/Patch.py", line 16 searchObj = re.findall(r patches[1:], output, re.M|re.I) ^ SyntaxError: invalid syntax Process finished with exit code 1
Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Yes, it's the syntax error. Any other way to pass list values as patterns to re.search or re.findall ?
Reply
#4
you need to use RegEx. If you are not familiar with regex expression syntax read https://docs.python.org/3/library/re.htm...ion-syntax and also https://www.regular-expressions.info/
you can use http://regex101.com to test your regex expression
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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
Reply
#6
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???
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(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()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
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")
    
Reply
#9
(Oct-20-2018, 05:57 AM)alinaveed786 Wrote: 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")
    

You convert patches elements to strings twice - would not it have been easier to just define them as strings?!
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  regex findall() returning weird result Radical 1 642 Oct-15-2023, 08:47 PM
Last Post: snippsat
  Python: re.findall to find multiple instances don't work but search worked Secret 1 1,209 Aug-30-2022, 08:40 PM
Last Post: deanhystad
  regex.findall that won't match anything xiaobai97 1 2,010 Sep-24-2020, 02:02 PM
Last Post: DeaD_EyE
  Regex findall() NewBeie 2 4,288 Jul-10-2020, 12:19 PM
Last Post: DeaD_EyE
  re.findall HELP!!! only returns None Rusty 10 6,977 Jun-20-2020, 12:13 AM
Last Post: Rusty
  The "FindAll" Error BadWhite 6 4,367 Apr-11-2020, 05:59 PM
Last Post: snippsat
  Beginner question: lxml's findall in an xml namespace aecklers 0 2,906 Jan-22-2020, 10:53 AM
Last Post: aecklers
  [Regex] Findall returns wrong number of hits Winfried 8 5,793 Aug-23-2018, 02:21 PM
Last Post: Winfried
  Combining the regex into single findall syoung 0 2,524 May-28-2018, 10:11 AM
Last Post: syoung
  unable to print the list when using re.findall() satyaneel 5 4,114 Sep-27-2017, 10:26 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020