afssfr
afssfr

Reputation: 1

get port without getting ip from regex

I need to retrieve port number not ip address from log file using regex. my log file has below structure:

ip: 123.23.32.1, port: 535

if i use (6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3}, then it retrieves 123 23 32 1 and 535, i want to retrieve port number, how to do?

i use regex

i use (6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]\d{4}|[1-9]\d{0,3}

I expects to retrieve port number only

Upvotes: 0

Views: 75

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522561

Assuming each line really looks like this:

ip: 123.23.32.1, port: 535

Demo

then you may use the regex pattern \d+$ to match the port number. This assumes, of course, that each line would look like this, and would be ending in the port number.

A more general pattern which would be more robust would be to use:

port: (\d+)

and then access the capture group (\d+) after the regex match.

Upvotes: 1

Jonathon K
Jonathon K

Reputation: 339

Anchor it to the end of the string with $ at the end of your pattern.

Upvotes: 0

Related Questions