2

I am trying to get an overview of the visitors of my website by using AWS Logs Insights.

My query looks like this:

fields @timestamp, @message
  | parse @message /(?<@ip>(?<=User-Agent)(.*)(?=X-Forwarded-Proto))/
  | stats count() as requestCount by @ip
  | filter ispresent(@ip)
  | sort requestCount desc

Some of the results are like this:

=Mozilla/5.0 (iPhone; CPU iPhone OS 15_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Mobile/15E148 Safari/604.1,
=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1 Safari/605.1.15,

I am trying to get the string within the first parenthesis:

  • iPhone; CPU iPhone OS 15_1 like Mac OS X
  • Macintosh; Intel Mac OS X 10_15_7

I tried | parse @ip /(?<@device>(/\((.*?)\)/)/ from this answer but it doesn't work.

Any ideas how I could make it work?

Thank you!

1 Answer 1

2

Looking at the result for the given pattern, you might use another named capture group (instead of a lookarounds, you might also match the text):

User-Agent=[^()]*\((?<@device>[^()]*)\).*X-Forwarded-Proto

See a regex demo.

With both capture groups:

User-Agent(?<@ip>[^()]*\((?<@device>[^()]*)\).*X-Forwarded-Proto)

See another regex demo.

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.