I’ve a regular expression problem, and I guess I’m missing a point in how the regex actually work.
I’ve some set of strings that contains methods definitions
- myMethod1()
- myMethod2(argument1 arg1)
- myMethod3(argument1 arg1, argument2 arg2)
but some of them also contains the output type:
- myOtherMethod1() : type1
- myOtherMethod2(argument1 arg1) : type2
- myOtherMethod3(argument1 arg1, argument2 arg2) : whatever
I want to only have inputs like the first ones: take out the output paramaters.
I’ve taken out my regex hat, and I come up with some conditional regex:
(?([:]+)(.+(?=\s:))|(.+))
If I match a “:” character in my string, I take whatever is before the ” :”, if not I take all.
Theoretically this is correct, but it returns the whole line
If I change the regex to
(?([:]*)(.+(?=\s:))|(.+))
Then the second type of methods are correctly regexed, but not the first ones (strange..).
Can you explain me where is my mistake?
Thank you very much,
There’s no need to handle it like this. Just take up to the first right parentheses you encounter:
Unless I’m misunderstanding your problem…