I try to write c# application and try to use regex to match function argument from function declaration. For example, I have
ReturnType Condition_Check(NegativeResponseCode *ErrorCode_pt, uint8 Sid_u8)
FUNC(Std_ReturnType, DiaDcmAdapter_CODE) ProductionMode_Write
(P2CONST(uint8, AUTOMATIC, RTE_APPL_DATA) p_Data,
P2VAR(Dcm_NegativeResponseCodeType, AUTOMATIC, RTE_APPL_DATA) ErrorCode)
void DSDL_V(const UBYTE* abc)
I want to match their's argument. Example for first function, it should return ErrorCode_pt and Sid_u8 for second function, it should return p_Data and ErrorCode for third function, it should return abc Not match if function don't have argument
It's easy for us with first function by use below regex
Condition_Check\(\w \s \*?(\w )\s*,\s*\w \s*(\w )\s*\)
My test: https://regex101.com/r/thD5bR/1
and get $1 and $2 But for 2nd function and 3rd function to complicated and more over, I don't know how many argument in the function (sometime it's 1 arg, sometime 2 arg...)
Is there any best way to match function argument from function declaration?
CodePudding user response:
You can match your function parameters by imposing two conditions:
(?<=[^,][ *])
- it is preceeded by no commas and either space or star(?=\)|,)
- it is followed by either a closed parenthesis or a comma
Here's the full regex:
(?<=[^,][ *])\w (?=\)|,)
Check the demo here.