2

I am trying to write a regular expression that match any of the following lines

localhost.localdomain (127.0.0.1) 0.025 ms 0.007 ms 0.006 ms

127.0.0.1 (127.0.0.1) 0.025 ms 0.007 ms 0.006 ms

I tried the follwing regexp

[127.0.0.1|localhost.localdomain] (127.0.0.1) [\d.]+ ms [\d.]+ ms [\d.]+ ms

But this doesn't work. It matches only

n (127.0.0.1) 0.025 ms 0.007 ms 0.006 ms

Can I get some help on this.

Thanks ~S

Community
  • 1
  • 1
user2677279
  • 127
  • 3
  • 10

1 Answers1

0

In character class, there is no concept of matching a string. For regex [cat], it does not mean that it should match the word cat literally. It means that it should match either c or a or t.

Same case is here. You are trying to match the starting string using character class

[127.0.0.1|localhost.localdomain]

It should be replaced with groups instead

(?:127.0.0.1|localhost.localdomain)

Also you want to match parenthesis () literally, but parenthesis have special meaning in regex. They are groups. To match parenthesis literally, it should be escaped using \.

So the final regex would be

(?:127.0.0.1|localhost.localdomain) \(127.0.0.1\) [\d.]+ ms [\d.]+ ms [\d.]+ ms

(?:) is non-capturing group. It doesn't store the result in memory.

Regex Demo

rock321987
  • 10,942
  • 1
  • 30
  • 43