user3455953
user3455953

Reputation: 23

Regex matching final occurrence of a pattern

I'm trying to match the final occurrence of a pattern of WWNs like this:

addr: 10:00:00:00:c4:a2:42:15
port: 10:00:00:00:c4:a2:42:15
addr: 10:00:00:00:c4:a2:42:16
port: 10:00:00:00:c4:a2:42:16

When I tested this:

port: (([0-9a-f]{2}[:-]){7}([0-9a-f]{2}))(?!.*port: ([0-9a-f]{2}[:-]){7}([0-9a-f]{2}))

it matches only 10:00:00:00:c4:a2:42:15. I thought my regex is non-greedy, but it looks like I'm still missing something.

Upvotes: 0

Views: 70

Answers (1)

Jerry
Jerry

Reputation: 71538

. doesn't match newlines. Try using [\s\S] instead:

port: (([0-9a-f]{2}[:-]){7}([0-9a-f]{2}))(?![\s\S]*port: ([0-9a-f]{2}[:-]){7}([0-9a-f]{2}))
                                            ^^^^^^

rubular demo

Upvotes: 6

Related Questions