1

I've strings like

con(tRegionDefaults.committee_chairman,', Secretary & Treasurer')
con('Non-Voting Member - ',tRegionDefaults.corptypelong,' Manager')
con('Vice ',tRegionDefaults.committee_chairman)

Tried to make a regex for getting all function parameters from each of the examples (including single quotes where the argument is a string), however with no success.

What I came upon was this regex but it doesn't give me groups of the function parameters:

([\(])([^,]+)+(\))

Can anyone give hint how to do that?

8
  • Unless you need to allow nested parentheses, just get everything from ( to ). If you have to allow nested parentheses, you need to write a real parser, regular expressions are not the right tool. Commented Jun 13, 2017 at 17:38
  • @Barmar added a regex I tried Commented Jun 13, 2017 at 17:41
  • A regexp can't return a variable number of groups. You can't match each parameter in a different group. Commented Jun 13, 2017 at 17:42
  • If you follow a capture group with + or *, it just captures the first match. Commented Jun 13, 2017 at 17:43
  • BTW, you don't need to put \( inside []. Commented Jun 13, 2017 at 17:43

2 Answers 2

1

Pattern: '[^']+'|(?<=con\(|,)[^,]+(?=,|\))

Pattern Explanation:

'[^']+'  #Match substrings that are single quoted<br>

|        # or

(?<=con\(|,)[^,]+(?=,|\))  
#Match non-comma characters preceded by con( or comma AND followed by a comma or )
Sign up to request clarification or add additional context in comments.

Comments

1

Put the capture group around all the arguments, not a single argument.

\((.*)\)

It's not possible to get each argument in a separate capture group, unless you're willing to limit the number of arguments that you capture. Capture groups are determined statically from the regexp by counting the parentheses -- a capture group inside a repetition quantifier still only captures one thing (its first match, I believe).

So use the above regexp to capture all the arguments, then use some other code to split the captured text at , characters.

1 Comment

Thanks for your input. The tricky part is that there are commas inside of the string parameters too, and that makes it difficult to parse

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.