EN VI

C# - Regex to capture 2 bits of data in - ABC:(anything)?

2024-03-14 01:30:04
How to C# - Regex to capture 2 bits of data in - ABC:(anything)

I need to parse (extract data - don't need to replace anything).

Test string: MAIN_HEADER:(ANOTHER_HEADER([DUMDUM:(BAH_123),OOPS,YEAH:(GAGA)])) need to output:

  • First component: MAIN_HEADER - only caps alpha and underscore

  • Second component: ANOTHER_HEADER:([DUMDUM:(BAH_123),OOPS,YEAH:(GAGA)]) - caps alpha, numbers, square and round brackets, commas, colons in any combination

I can extract second component with ^\(([a-zA-Z0-9_:,\(\)\[\]]*)\)$ but I don't know how to do 2 different matches to split components.

Solution:

You may use this regex to extract 2 capture group as desired:

^([A-Z_]+):\s*\(([][A-Z0-9_(),:]+)\)

RegEx Demo

RegEx Details:

  • ^:
  • ([A-Z_]+): Match 1+ of uppercase letters or _ in capture group #1
  • :\s*: Match : followed by 0 or more whitespaces
  • \(: Match literal (
  • ([][A-Z0-9_(),:]+): Match 1+ of uppercase letters or _ or digits or ( or ) or [ or ] or , or : in capture group #2
  • \): Match literal )
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login