• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: How do I separate 2 groups with a regex?

Resolved: How do I separate 2 groups with a regex?

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

First time asking a question. So, I’m learning regex and I got this exercise:
input string:
Retreat the following two groups using a regex to get the following:
and
avoiding the colon : after CommunicationsException.
My solution is:
It worked, but I’m sure its really messy. Could anybody explain to me how to upgrade it?

Answer:

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:
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.

If you have better answer, please add a comment about this, thank you!

grouping match regex
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How do I stop the command from happening if the requirements for it to work aren’t sert Discord.js

02/04/2023

Resolved: How to scroll bottom of div at launch?

02/04/2023

Resolved: how to get and read an xml file in a zip file using xml.etree

02/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.