pats4u
pats4u

Reputation: 271

Regex to extract text after a pattern

I am trying to extract the Error Messages from loader output like below :

LOADER_ERROR : *** Failure The UserId, Password or Account is invalid., attempts:1
LOADER_ERROR : *** Time out waiting for a response.

Expected Output :

Failure The UserId, Password or Account is invalid., attempts:1
Time out waiting for a response.

With the below regex, I am able to extract everything after the last occurrence of ': ' character.

  .+(\:\s.+)$

Output :

: *** Failure The UserId, Password or Account is invalid., attempts:1
: *** Time out waiting for a response.

How do I strip ': ' or '*** ' at the beginning from the output ?

Thanks for any assistance

Upvotes: 2

Views: 5371

Answers (2)

The fourth bird
The fourth bird

Reputation: 163207

The data that you want seems to be after the first colon. You can match all before the first colon using a negated character class [^

Note that you don't have to escape \:

^[^:]+:\W*(.+)$

The pattern matches:

  • ^ Start of string
  • [^:]+: Match 1+ times any char except :, then match :
  • \W* Optionally match non word charcters
  • (.+) Capture in group 1, matching 1+ times any char
  • $ End of string

Regex demo


If the format of the data is always like that, a more strict pattern could be matching 3 times an asterix and start the capture group with matching a word character.

^\w+\s+:\s+\*{3}\s+(\w.*)$

Regex demo

Upvotes: 3

Charchit Kapoor
Charchit Kapoor

Reputation: 9284

You are including the : and *** that's in the group so they will come in the output. This should work:

.+\:\s\W+(.+)$

Check its demo here.

Upvotes: 4

Related Questions