user2806964
user2806964

Reputation:

Match a code string in some text

I have some code strings that I need to extract some data from, but specific data at that.

The strings are always in the same format.

  1. I need to extract the text at the beginning between the ( and ), so it would extract List Options here.
  2. I need to extract the text near the end @groups here.

The string I need at the end will always start with @

(List Options((join ", ", @groups))

I have tried:

^\((\w+).*, (@\w+)\)\)$

But it only gives me the word List

Upvotes: 1

Views: 49

Answers (2)

hwnd
hwnd

Reputation: 70732

This should work well for you.

^\(([^(]+)[^@]+([^)]+)\)+$

See working demo

Regular expression:

^          the beginning of the string
\(         look and match '('
 (         group and capture to \1:
  [^(]+    any character except: '(' (1 or more times)
 )         end of \1
  [^@]+    any character except: '@' (1 or more times)
 (         group and capture to \2:
  [^)]+    any character except: ')' (1 or more times)
 )         end of \2
 \)+       ')' (1 or more times)
$          before an optional \n, and the end of the string

Upvotes: 2

Dmitry Avtonomov
Dmitry Avtonomov

Reputation: 9529

try this one

^\(([^\(]+?)\(.*?@([^\)]+?)\)

or if you need the @ sign also captured, just move it inside the 2nd capturing group

^\(([^\(]+?)\(.*?(@[^\)]+?)\)

Upvotes: 1

Related Questions