Fenomatik
Fenomatik

Reputation: 477

Any character with carriage return

New to Regex and I believe I am working on regex for java with one problem that the lines have carriage return in them which makes (.*) (the last group) matches everything except new line.

^([0-9]{2}\s[A-Za-z]{3}\s[0-9]{4}\s[0-9]{2}:[0-9]{2}:[0-9]{2}(?:,[0-9]{3})?)\s(?:\^\[\[[0-9]{2}m)\[([A-Za-z]+)\](?:\^\[\[m)\s*([0-9a-fA-F\-]*)\s\(([^\)]+)\)\s((?:[a-zA-Z0-9-]+\.)+[A-Za-z0-9$]+):\s(?:\s\-\s)?(.*)

Not matching the 2 lines following the first one but picks up the forth one

02 Jan 2018 05:25:56,546 ^[[32m[TEXT]^[[m aabb33-ddee33-54321 (host-1-usa-east) this.is.sample.log: service is responding normal
extra line 1
extra line 2
03 Jan 2018 08:25:56,546 ^[[32m[TEXT]^[[m aabb33-ddee33-54321 (host-1-usa-east) this.is.sample.log: service is responding normal

Is there a way to have all matched along with carriage return.

UPDATE:

Following regex isnt picking up the last group

 ^([0-9]{2}\s[A-Za-z]{3}\s[0-9]{4}\s[0-9]{2}:[0-9]{2}:[0-9]{2}(?:,[0-9]{3})?)\s(?:\^\[\[[0-9]{2}m)\[([A-Za-z]+)\](?:\^\[\[m)\s*([0-9a-fA-F\-]*)\s\(([^\)]+)\)\s((?:[a-zA-Z0-9-]+\.)+[A-Za-z0-9$]+):(?:\s\-\s)?(?:(?!^[0-9]{2}\s[A-Za-z]{3}\s[0-9]{4}\s[0-9]{2}:[0-9]{2}:[0-9]{2}).*(?:\r?\n)?)*

 Service is responding normal
Test results 1 = True
Test results 2 = "{False}"


10 Aug 2018 06:00:02,152 ^[[32m[TEST]^[[m aabb345-397c-4656 (host-1-usa-east) this.is.sample.log: Service is responding normal
    Test results 1 = True
    Test results 2 = "{False}"
    10 Aug 2018 06:00:02,152 ^[[32m[TEST]^[[m aabb345-397c-4656 (host-1-usa-east) this.is.sample.log: Service is responding normal

https://regex101.com/r/wc529u/2

Upvotes: 1

Views: 89

Answers (1)

The fourth bird
The fourth bird

Reputation: 163632

You could replace the last (.*) with (?:(?!^[0-9]{2}\s[A-Za-z]{3}\s[0-9]{4}\s[0-9]{2}:[0-9]{2}:[0-9]{2}).*(?:\r?\n)?)* as suggested by Sebastian Proske which uses a negative lookahead (?! to assert that what follows is not the pattern you specify in your regex and the match any character until the end of the string and match an optional line break.

Explanation of the replaced part

  • (?: Non capturing group
    • (?! Negative lookahead
      • ^[0-9]{2}\s[A-Za-z]{3}\s[0-9]{4}\s[0-9]{2}:[0-9]{2}:[0-9]{2} Pattern that matches the start of the string
      • ).* Close negative lookahead and match any character zero or more times
    • (?:\r?\n)? Match an optional line break
  • )* Close the non capturing group and repeat zero or more times

Your regex might look like:

^([0-9]{2}\s[A-Za-z]{3}\s[0-9]{4}\s[0-9]{2}:[0-9]{2}:[0-9]{2}(?:,[0-9]{3})?)\s(?:\^\[\[[0-9]{2}m)\[([A-Za-z]+)\](?:\^\[\[m)\s*([0-9a-fA-F\-]*)\s\(([^\)]+)\)\s((?:[a-zA-Z0-9-]+\.)+[A-Za-z0-9$]+):\s(?:\s\-\s)?(?:(?!^[0-9]{2}\s[A-Za-z]{3}\s[0-9]{4}\s[0-9]{2}:[0-9]{2}:[0-9]{2}).*(?:\r?\n)?)*

Upvotes: 1

Related Questions