2017-03-06 23 views
1

複数行の正規表現に一致するのに問題があります。 私はいくつかを試みたが運がない。複数行に一致するREGEX

まず試し: ((?:\ bは#ショー)(:?。*?\ n)は{6})

結果:に失敗しました。ラインが5〜8の間にあることがあることが分かった。 6回のマッチングはうまくいきません。

2回目の試行:(?< =#の\ nを)(。?ショー*バージョン)

結果:失敗しました:私は成功と同様の正規表現を使用しましたが、何にも一致していません他のマッチ。

文字列私は一致しようとしています。

wgb-car1# show startup-config 
Using 6149 out of 32768 bytes 
! 
! NVRAM config last updated at 15:50:05 UTC Wed Oct 1 2014 by user 
! 
version 12.4 
no service pad 
service timestamps debug datetime msec 
service timestamps log datetime msec 
service password-encryption 
! 

私はショーからバージョン番号にすべてを一致させるためにしようとしています。

この正規表現は、(?s)は#ショーの作品(。*)バージョンが、私は、彼らが小数点以下のいずれかの組み合わせとすることができるよう番号を取得する方法を知っているが、常に数字はありません。

答えて

1

あなたは、次の正規表現使用することができます

(?s)#\sshow\s*(.*?)version\s*([\d.]+) 

DEMO

のpythonをdemo

import re 

s = """wgb-car1# show startup-config 
Using 6149 out of 32768 bytes 
! 
! NVRAM config last updated at 15:50:05 UTC Wed Oct 1 2014 by user 
! 
version 12.4 
no service pad 
service timestamps debug datetime msec 
service timestamps log datetime msec 
service password-encryption 
!""" 
r = r"(?s)#\sshow\s*(.*?)version\s*([\d.]+)" 
o = [m.group() for m in re.finditer(r, s)] 
print o 
+0

私が探していたまさに、ありがとうございました。 – NineTail

+0

歓迎します:-) – m87

0

バージョン番号まで改行をマッチングしてみて、それからm後でニューラインを踏む。 (?sm:show.*\nversion)を使用すると、複数行の動作((?sm:...)設定)を取得し、その後は.*$のような何かをマルチライン化することができます。

0

1つの回答(特に)はposを使用します。先読み:

\#\ show 
([\s\S]+?) 
(?=version) 

a demo on regex101.comを参照してください。


はフル Python例として:

import re 

string = """ 
wgb-car1# show startup-config 
Using 6149 out of 32768 bytes 
! 
! NVRAM config last updated at 15:50:05 UTC Wed Oct 1 2014 by user 
! 
version 12.4 
no service pad 
service timestamps debug datetime msec 
service timestamps log datetime msec 
service password-encryption 
!""" 

rx = re.compile(r''' 
    \#\ show 
    ([\s\S]+?) 
    (?=version) 
    ''', re.VERBOSE) 

matches = [match.group(0) for match in rx.finditer(string)] 
print(matches) 
# ['# show startup-config\nUsing 6149 out of 32768 bytes\n!\n! NVRAM config last updated at 15:50:05 UTC Wed Oct 1 2014 by user\n!\n'] 
関連する問題