J Andrew McCormick
J Andrew McCormick

Reputation: 314

Regex pattern to parse string

So I need to find a regex to parse a few different cases with the same regex.

First case:

Type(arg1, arg2)

I need to get Type, arg1, and arg2 out of this string.

Second Case:

Type(arg1, arg2, ...) (the number of args is variable)

I need to get Type, arg1, arg2, and every other arg.

Third Case:

Type(arg1 + arg2) or Type(arg1 - arg2) or (lets just say that it can take really any form)

Here I need Type and arg1 + arg2

Note: I'm not opposed to using two regex, meaning if I could get "Type" and anything inside the parenthesis with one regex and then pulling the comma separated args apart using a second regex that would be ok, just not preferred.

Upvotes: 0

Views: 100

Answers (1)

Federico Piazza
Federico Piazza

Reputation: 30985

You can use a regex like:

(\w+\s*[+-]?\s*\w+)

Working demo

Update: if you pattern can take whatever form, then you can use this pattern:

([^(),\n]+)

The idea is to match whatever thing except parentheses, commas and new lines

Upvotes: 1

Related Questions