Home > Software design >  How do I separate 2 groups with a regex?
How do I separate 2 groups with a regex?

Time:06-17

First time asking a question. So, I'm learning regex and I got this exercise:

input string:

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Retreat the following two groups using a regex to get the following:

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException

and

Communications link failure

avoiding the colon : after CommunicationsException.

My solution is:

Caused\s by:\s [A-Za-z.] \d[.] [A-Za-z] |\s ... 

It worked, but I'm sure its really messy. Could anybody explain to me how to upgrade it?

CodePudding user response:

You can match Cause by: and then match all text up to the next colon, and then start matching from any non-whitespace that is not a colon till the end of string:

Caused\s by:[^:] |[^:\s].*

See the regex demo. If the match is at the start of the string, add ^ at the start.

As an alternative, if you do not care what text is at the string beginning, you can even use ^[^:] :[^:] |[^:\s].*.

Details:

  • ^ - start of string
  • Caused\s by: - Caused by: with any one or more whitespaces between Caused and by
  • [^:] - one or more chars other than :
  • | - or
  • [^:\s] - a char other than whitespace and :
  • .* - the rest of the line.
  • Related