0

We have dig output which looks like

Trying "google.com"
Using domain server:
Name: 177.39.160.253
Address: 177.39.160.253#53
Aliases: 

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 52549
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 13, ADDITIONAL: 6

;; QUESTION SECTION:
;google.com.            IN  A

;; ANSWER SECTION:
google.com.     245 IN  A   216.58.202.46
google.com.     245 IN  A   216.58.202.47

;; AUTHORITY SECTION:
com.            16060   IN  NS  d.gtld-servers.net.
com.            16060   IN  NS  e.gtld-servers.net.
com.            16060   IN  NS  g.gtld-servers.net.
com.            16060   IN  NS  c.gtld-servers.net.
com.            16060   IN  NS  m.gtld-servers.net.
com.            16060   IN  NS  f.gtld-servers.net.
com.            16060   IN  NS  i.gtld-servers.net.
com.            16060   IN  NS  l.gtld-servers.net.
com.            16060   IN  NS  k.gtld-servers.net.
com.            16060   IN  NS  a.gtld-servers.net.
com.            16060   IN  NS  j.gtld-servers.net.
com.            16060   IN  NS  b.gtld-servers.net.
com.            16060   IN  NS  h.gtld-servers.net.

;; ADDITIONAL SECTION:
a.gtld-servers.net. 13518   IN  A   192.5.6.30
e.gtld-servers.net. 23554   IN  A   192.12.94.30
e.gtld-servers.net. 24306   IN  AAAA    2001:502:1ca1::30
g.gtld-servers.net. 13443   IN  AAAA    2001:503:eea3::30
k.gtld-servers.net. 2284    IN  A   192.52.178.30
k.gtld-servers.net. 10638   IN  AAAA    2001:503:d2d::30

Received 400 bytes from 177.39.160.253#53 in 306 ms

I need to write regex which will return 216.58.202.46 and 216.58.202.47

Right now I'm doing it using 2 regexes:

At first I'm taking content of the answer section with

;; ANSWER SECTION:[\s\S]*?\n\n

Then I'm parsing answer with

.*IN[\t *]A[\t *](?P<output>[\S]*)\n

Can I achieve same functionality with 1 regular expression? Thanks.

7
  • 1
    Try regex101.com/r/2Np9Mr/1 Commented Jun 28, 2017 at 8:08
  • @WiktorStribiżew wow! Even with the regex101 explanation I still don't quite get all you did ... I'll need some time to figure out everything Commented Jun 28, 2017 at 8:18
  • @Esteban: If it works for OP, I will post with explanations. Илья, does it work for you? Commented Jun 28, 2017 at 8:19
  • @WiktorStribiżew It works on his exampe for sure, only thing I changed on the regex was the ADDITIONAL SECTION to ANSWER SECTION, but all in all, it did what he wants to. Thanks for taking time to make the explanation Commented Jun 28, 2017 at 8:26
  • 1
    Yes, regex101.com/r/2Np9Mr/2. I did not pay attention to that bit. Commented Jun 28, 2017 at 8:27

1 Answer 1

2

You may use a regex based on the \G operator that matches either the position at the start of a string, or the location after a successful match.

(?s)(?:\G(?!\A)|;; ANSWER SECTION:)(?:(?!\R{2}).)*? IN +A +\K\S+

See the regex demo.

Details:

  • (?s) - an inline DOTALL modifier that makes . match line break chars, too (now, it matches any chars)
  • (?:\G(?!\A)|;; ANSWER SECTION:) - either the location after the previous match (\G(?!\A), the start of string position is subtracted with the negative lookahead) or a literal string ;; ANSWER SECTION:
  • (?:(?!\R{2}).)*? - a tempered greedy token matching any char (.) that is not a starting point for a double line break sequence (\R{2}, \R matches any linebreak sequence), as few times as possible, as *? is a lazy quantifier
  • IN +A + - space, IN, 1+ spaces, A, 1+ spaces
  • \K - a match reset operator
  • \S+ - one or more non-whitespace chars.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.