在正则表达式python中使用for循环作为模式

时间:2018-10-18 12:49:21

标签: python regex python-3.x

下面是我苦苦挣扎的代码。它检查Oracle home中的所有补丁,并提供是否应用的信息。 问题在于它甚至会打印甚至不存在的补丁“ 12345”。

此外,我们是否可以使用 for循环传递列表变量“ patches”的值,并对照变量“ output”检查每个补丁

#!/usr/bin/python

import re
import subprocess

s = subprocess.check_output(["opatch", "lsinventory"])
output = s.decode("utf-8")

patches = [27923320, 27547329, 21463894, 12345]

searchObj = re.findall(str(patches), output)
if searchObj:
        print('Patch', patches, "detected")
else:
        print("Patch", patches, "Not found!!")

输出:

Patch [27923320, 27547329, 21463894, 12345] detected

变量“输出”的内容

[oracle@cdb1 ~]$ opatch lsinventory
Oracle Interim Patch Installer version 12.2.0.1.14
Copyright (c) 2018, Oracle Corporation.  All rights reserved.


Oracle Home       : /u01/app/oracle/product/12.1.0.2/db_1
Central Inventory : /u01/app/oraInventory
   from           : /u01/app/oracle/product/12.1.0.2/db_1/oraInst.loc
OPatch version    : 12.2.0.1.14
OUI version       : 12.1.0.2.0
Log file location : /u01/app/oracle/product/12.1.0.2/db_1/cfgtoollogs/opatch/opatch2018-10-18_19-16-18PM_1.log

Lsinventory Output file location : /u01/app/oracle/product/12.1.0.2/db_1/cfgtoollogs/opatch/lsinv/lsinventory2018-10-18_19-16-18PM.txt

--------------------------------------------------------------------------------
Local Machine Information::
Hostname: cdb1.naveed.com
ARU platform id: 226
ARU platform description:: Linux x86-64

Installed Top-level Products (1): 

Oracle Database 12c                                                  12.1.0.2.0
There are 1 products installed in this Oracle Home.


Interim patches (6) :

Patch  21463894     : applied on Wed Oct 17 21:34:48 IST 2018
Unique Patch ID:  19210803
   Created on 6 Aug 2015, 07:07:33 hrs PST8PDT
   Bugs fixed:
     21463894

Patch  21171382     : applied on Wed Oct 17 21:32:02 IST 2018
Unique Patch ID:  22292308
   Created on 27 Jun 2018, 10:48:17 hrs PST8PDT
   Bugs fixed:
     21171382
   This patch overlays patches:
     27338041
   This patch needs patches:
     27338041
   as prerequisites

Patch  28432129     : applied on Wed Oct 17 21:30:30 IST 2018
Unique Patch ID:  22370233
   Created on 7 Aug 2018, 18:32:01 hrs PST8PDT
   Bugs fixed:
     28432129
   This patch overlays patches:
     27547329
   This patch needs patches:
     27547329
   as prerequisites

Patch  18961555     : applied on Wed Oct 17 21:29:01 IST 2018
Unique Patch ID:  22141716
   Created on 25 Apr 2018, 15:18:56 hrs PST8PDT
   Bugs fixed:
     18961555
   This patch overlays patches:
     27338041
   This patch needs patches:
     27338041
   as prerequisites

Patch  27547329     : applied on Wed Oct 17 21:22:51 IST 2018
Unique Patch ID:  22280349
Patch description:  "Database Patch Set Update : 12.1.0.2.180717 (27547329)"
   Created on 22 Jun 2018, 10:43:01 hrs PST8PDT
Sub-patch  27338041; "Database Patch Set Update : 12.1.0.2.180417 (27338041)"
Sub-patch  26925311; "Database Patch Set Update : 12.1.0.2.180116 (26925311)"
Sub-patch  26713565; "Database Patch Set Update : 12.1.0.2.171017 (26713565)"
Sub-patch  26609783; "Database Patch Set Update : 12.1.0.2.170814 (26609783)"
Sub-patch  25755742; "Database Patch Set Update : 12.1.0.2.170718 (25755742)"
Sub-patch  25171037; "Database Patch Set Update : 12.1.0.2.170418 (25171037)"
Sub-patch  24732082; "Database Patch Set Update : 12.1.0.2.170117 (24732082)"
Sub-patch  24006101; "Database Patch Set Update : 12.1.0.2.161018 (24006101)"
Sub-patch  23054246; "Database Patch Set Update : 12.1.0.2.160719 (23054246)"
Sub-patch  22291127; "Database Patch Set Update : 12.1.0.2.160419 (22291127)"
Sub-patch  21948354; "Database Patch Set Update : 12.1.0.2.160119 (21948354)"
Sub-patch  21359755; "Database Patch Set Update : 12.1.0.2.5 (21359755)"
Sub-patch  20831110; "Database Patch Set Update : 12.1.0.2.4 (20831110)"
Sub-patch  20299023; "Database Patch Set Update : 12.1.0.2.3 (20299023)"
Sub-patch  19769480; "Database Patch Set Update : 12.1.0.2.2 (19769480)"
   Bugs fixed:
     19309466, 19902195, 18250893, 25437699, 19383839, 16756406, 18456643
     26546664, 18845653, 19915271, 20172151, 18417036, 23713236, 24796092
     19243521, 19658708, 21153266, 19174430, 22243719, 20688221, 21387964
     13542050, 22734547, 21623164, 19012119, 19932634, 19869255, 22232606
     18681056, 23324000, 25427662, 22068305, 24589081, 19439759, 19303936
     22916353, 24835538, 22353346, 21106027, 26444887, 23088803, 22529728
     26256131, 19134173, 20447445, 21188584, 19390567, 26513709, 19769480
     21097043, 21225209, 20677396, 19284031, 26245237, 19450314, 19016730
     20919320, 22075064, 22551446, 22721409, 18440095, 22496904, 16439813
     18354830, 20596234, 22022760, 20936905, 23197103, 21514877, 26111842
     18990023, 22492533, 20173897, 24624166, 17210525, 21260431, 20181030
     25056052, 19370504, 21868720, 23068169, 19124589, 19402853, 19888853
     24341675, 17722075, 20882568, 23026585, 25653109, 20717081, 25546608
     19081128, 27370965, 22173980, 19178851, 20951038, 22168163, 25161298
     20569094, 24308635, 19791377, 19050649, 20920911, 19189525, 19469538
     20598042, 22458049, 18988834, 17409174, 22729345, 22842151, 19238590
     16941434, 20387265, 24397438, 20673810, 23108128, 20356733, 22380919
     18436647, 23065323, 20825533, 19124336, 22294260, 24790914, 20284155
     25539063, 17365043, 20952966, 22961508, 19176223, 21300341, 23237313
     18288842, 22353199, 22083366, 21419850, 26898563, 19577410, 23294548
     19931709, 25423453, 25547060, 23533807, 24600330, 25600421, 18122373
     20043616, 23124895, 18856999, 21450666, 18893947, 20076781, 26029780
     21196809, 21354456, 20464614, 23725036, 19562381, 24808595, 19189317
     18307021, 25669791, 21917884, 19708632, 27213224, 25633101, 20711718
     18973548, 25982666, 22826718, 25655390, 21773465, 20250147, 19197175
     19597439, 26263721, 21387128, 19180770, 19879746, 21785691, 20424183
     24285405, 26544823, 20322560, 22228324, 22520320, 23172924, 21575362
     22365117, 22645009, 25165496, 18774543, 20124446, 21429602, 19371175
     21863727, 18940497, 19074147, 22923409, 21380789, 19154375, 19044962
     19532017, 19662635, 22374754, 20560611, 25654936, 21492036, 18705806
     19578247, 22024071, 22238921, 22809871, 21184223, 23089357, 19404068
     18921743, 19065677, 19018447, 19018206, 18308268, 19777862, 22223463
     19304354, 22519146, 27199245, 20890311, 21142837, 20869721, 24555417
     22179537, 21756699, 20217801, 18819908, 22760595, 25483815, 23007241
     19593445, 21080143, 20031873, 18618122, 26784509, 24739928, 18966843
     19077215, 20704450, 19068970, 20543011, 19023822, 24713381, 20432873
     21756677, 20328248, 18674047, 18849537, 25459958, 20315311, 22897344
     27534509, 25178179, 19308965, 18948177, 19468991, 20868862, 21780146
     20466628, 21756661, 20397490, 19706965, 24831514, 23240358, 22178855
     20302006, 19032777, 20862087, 19329654, 18974476, 20603378, 20859910
     19307662, 21847223, 20281121, 19075256, 19076343, 18866977, 20844426
     20904530, 20441797, 21442094, 25079710, 24674955, 18840932, 18740837
     20294666, 25602488, 21517440, 22062517, 27337759, 19174942, 20671094
     21889720, 18411216, 20117253, 24386767, 20641666, 25264559, 22092979
     21625179, 20879709, 23003979, 20165574, 19272708, 19547370, 22624709
     23084507, 20228093, 21281532, 19805359, 19461270, 19434529, 18799063
     20378086, 17008068, 21246723, 20831538, 20424899, 20361671, 18674024
     19689979, 24411921, 19873610, 16619249, 20562898, 21091431, 21641414
     19440586, 22757364, 22175564, 21241052, 19561643, 19399918, 19195895
     20830459, 20017509, 25790353, 21828126, 21665897, 20746251, 25764020
     25612095, 25357142, 23096938, 19067244, 18043064, 21329301, 18885870
     26187943, 20324049, 19536415, 20446883, 21299490, 27314206, 25313154
     21744290, 18254023, 20591183, 27847259, 19185876, 22465352, 27207110
     20402832, 19627012, 27441326, 27620950, 16863642, 19639483, 19315691
     21479753, 19174521, 20401975, 18306996, 18851894, 27034890, 20581111
     20318889, 20936731, 21060755, 22256560, 19188927, 24570598, 25475853
     21172913, 17655240, 21266085, 19028800, 19035573, 19366375, 24523374
     25034396, 19289642, 21291274, 18007682, 23521523, 20475845, 22148226
     22528741, 25417958, 24652769, 26088426, 19326908, 19597583, 17414008
     23019710, 20897759, 22046677, 20938170, 24825843, 21960504, 24509056
     19054077, 22657942, 26262953, 20428621, 21899588, 19723336, 19835133
     17532734, 19333670, 21842017, 19285025, 21373473, 23260854, 19687159
     14643995, 20977794, 20734332, 17551063, 27548131, 21977392, 24461826
     19676012, 20588502, 23315889, 19520602, 23053606, 19841800, 20245930
     19001359, 21476308, 26546754, 19393542, 23533524, 21099555, 25429959
     19141838, 19644859, 21915719, 19908836, 21421886, 19358317, 19524158
     23548817, 25861398, 20803014, 23025340, 19335438, 19058490, 19207117
     18799993, 26569225, 25919622, 20835241, 24662775, 19475971, 18967382
     20347562, 20348653, 19896336, 24812585, 20048359, 21896069, 19524384
     25392535, 20440930, 25789277, 19171086, 24718260, 17867700, 19791273
     21241829, 19591608, 22707244, 18419520, 22296366, 18914624, 19571367
     19501299, 20425790, 19708342, 27997875, 16870214, 18202441, 24415926
     18743542, 19001390, 21875360, 25091141, 19149990, 20382309, 22855193
     16777441, 19606174, 20848335, 25495682, 19382851, 20528052, 22762046
     24563422, 23125826, 22503297, 25192729, 23338911, 22730454, 19176326
     19048007, 18849970, 21532755, 20860659, 22905130, 21263635, 22160989
     18499088, 21059919, 18952989, 22518784, 25856821, 25484507, 20794034
     19468347, 17533661, 19883092, 20657441, 24401351, 21285458, 18051556
     25330273, 19699191, 24437510, 20669434, 18964978, 20828947, 21373076
     25551676, 14283239, 25766822, 22922076, 25575628, 20368850, 21239530
     20437153, 20880215, 20798891, 25606091, 19013183, 21133343, 22695831
     24365589, 19587324, 18542562, 26758193, 22062026, 20134339, 22077517
     22815955, 24690216, 22507210, 20101006, 21795111, 27938623, 23501901
     18797519, 21260397, 25029423, 19354335, 19730508, 22366558, 26658759
     6599380, 20717359, 24321547, 21297872, 18964939, 26366517, 21913183
     22366322, 20171986, 20603431, 21132297, 25957038, 21542577, 22507234
     23170620, 24719736, 25600342, 18868646, 20627866, 18110491, 16923858
     24642295, 19518079, 20466322, 25823754, 25110233, 24908321, 20842388
     17274537, 26575788, 20474192, 21644640, 21794615, 18899974, 20471920
     22806698, 19052488, 19503821, 24350620, 20074391, 19157754, 21220620
     24316947, 19865345, 19065556, 22816287, 25947799, 20878790, 23492665
     21322887, 20879889, 24350831, 19578350, 19363645, 21072646, 20898391
     19291380, 27060167, 27086138, 22536802, 22087683, 20373598, 19248799
     22707866, 19155797, 19279273, 18886413, 25490238, 20922010, 19990037
     25150925, 20509482, 24717859, 20703000, 22862134, 21526048, 24929210
     24560906, 20144308, 21620471, 19670108, 19068610, 20267166, 25123585
     20476175, 18549238, 22950945, 19385656, 23528412, 19684504, 21174504
     20899461, 20557786, 21911701, 19143550, 19024808, 20118035, 20009833
     25760195, 19604659, 16359751, 26039623, 19928926, 23314180, 20212067
     24737403, 20480209, 26430737, 27169796, 21668627, 20877664, 19487147
     23149541, 24577566, 19430401, 19676905, 20925795, 21296029, 21629064
     23229229, 22865673, 20708701, 19280225, 25353983, 21315084, 19213447
     19989009, 18191823, 24393981, 25639019, 17319928, 19703301, 21626377
     20122715, 6418158, 23105538, 26198926, 19258504, 21188532, 17890099
     21649497, 26446098, 16887946, 26024732, 18791688, 19721304, 19490948
     19619732, 21164318, 18090142, 21641760, 19818513, 20139391, 24693382
     19978542, 23543183, 22165897, 22359063, 19409212, 23035249, 18990693
     20470877, 21422580, 21632821, 22351572, 20235511, 23220453, 18604493
     23008056, 18610915, 20832516, 24801152, 26089440, 20907061, 20505778
     19183343, 21787056, 21273804, 25093739, 17835294, 24413809, 18371441
     24385983, 20413820, 26714910, 24421668, 25897615, 25643931, 21281607
     20513399, 23195445, 20558005, 20093776, 18909599, 20618595, 23572982
     19211433, 20331945, 19512341, 22256431, 19637186, 19022470, 18607546
     24573817, 19649152, 19201867, 21294938, 20898997, 18510194, 22454326
     19534363, 24683149, 25489607

Patch  27923320     : applied on Wed Oct 17 21:08:32 IST 2018
Unique Patch ID:  22224206
Patch description:  "Database PSU 12.1.0.2.180717, Oracle JavaVM Component (JUL2018)"
   Created on 29 May 2018, 11:24:41 hrs PST8PDT
   Bugs fixed:
     27923320, 27952584, 27952577, 27642235, 27475603, 27461789, 27461842
     25649873, 27001733, 27000663, 27000690, 26635845, 26637592, 26570171
     26027162, 26023002, 26023025, 25437695, 25494413, 25494379, 24917972
     25067795, 24534298, 25076732, 25076756, 24315824, 21659726, 24448240
     24448282, 23177536, 22675136, 23265914, 23265965, 23727148, 22674709
     22670413, 22670385, 21188537, 22139226, 22118835, 22118851, 21555660
     21811517, 19623450, 21566993, 21566944, 19176885, 21068507, 21047803
     21047766, 20415564, 20408829, 20408866, 19877336, 19855285, 19909862
     19895362, 19895326, 19153980, 19231857, 19223010, 19245191, 19699946



--------------------------------------------------------------------------------

OPatch succeeded.

1 个答案:

答案 0 :(得分:0)

在将|列表中的整数映射到字符串,然后将其包装在确保单词边界的正则表达式中之后,可以以patches的交替形式加入列表。

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")
    for patch in patches_missing:
        process(patch)