Match pattern only when the previous pattern matches

I have a situation where I only need to match a pattern when a previous regex pattern matches. Both patterns are different from each other and match each other on different lines. For exmaple

Text

blah blah blah MyHost="xxxx"
again blah blah blah MyIp= "x.x.x.x"

      

I'm only interested in what happens after MyHost

and MyIp

. I also have a requirement to MyIp

only match when the given line has match(MyHost="xxxx")

.

I can map both the value MyHost

and the value MyIp

separately, but have a hard time finding the logic to match as required. Please note, I am fairly new to python and have tried searching many times and end up here.

+3


source to share


5 answers


MyIp

should only match when the above line has match(MyHost="xxxx")

.

Get consistent group from index 1 in different ways. You already know what will happen afterMyHost

\bMyHost="xxxx"\r?\n.*?MyIp=\s*\"([^"]*)

      



Here is a demo

example code:

import re
p = re.compile(ur'\bMyHost="xxxx"\r?\n.*?MyIp=\s*\"([^"]*)', re.IGNORECASE)
test_str = u"blah blah blah MyHost=\"xxxx\"\nagain blah blah blah MyIp= \"x.x.x.x\""

re.findall(p, test_str)

      

+1


source


You can do this through the regular expression module.

>>> import regex
>>> s = '''blah blah blah MyHost="xxxx"
... foo bar
... again blah blah blah MyIp= "x.x.x.x"
... 
... blah blah blah MyHost="xxxx"
... again blah blah blah MyIp= "x.x.x.x"'''
>>> m = regex.search(r'(?<=MyHost="xxxx"[^\n]*\n.*?MyIp=\s*")[^"]*', s)
>>> m.group()
'x.x.x.x'

      

This will only match the value MyIp

if the line is MyHost="xxxx"

present on the previous line.



If you want to list both, try the code below.

>>> m = regex.findall(r'(?<=(MyHost="[^"]*")[^\n]*\n.*?)(MyIp=\s*"[^"]*")', s)
>>> m
[('MyHost="xxxx"', 'MyIp= "x.x.x.x"')]

      

+1


source


       (?=.*? MyHost=\"xxx\" .*) .*? MyIp=\"(\S+)\" .*

      

xxx can be changed as needed. MIP will be captured.

You can use python lookahead.Only when xxx matches regex will go ahead and get IP

         (?=regex)regex1

      

match regex1 only when the regex matches.

0


source


You should use short-circuiting , I believe python supports it.When short-circuiting, the second condition will only be evaluated if the first is true (for AND operations). So your code will look like this:

 patternMatch1(MyHost) and patternMatch2(MyIp)

      

Here you can use both true pattern matching functions if they match.

Please let me know if you have any questions!

0


source


In general, if you want to use Regex, you will need to match "MyHost" and everything that follows and "MyIP" and follow it to the end of the line

So, basically you want to write a regex like this

MyHost = "\ W +"

This will match MyHost = "" and the input between it will be set to W then you can get the value of W and do the calculations you need.

To solve the problem in which you need to match the First host simply if Condition can solve this problem by checking the hostname before Ip

0


source







All Articles