Regular expression to recognize variable declarations in C

后端 未结 5 1395
我寻月下人不归
我寻月下人不归 2021-01-03 11:50

I\'m working on a regular expression to recognize variable declarations in C and I have got this.

[a-zA-Z_][a-zA-Z0-9]*

Is there any bette

5条回答
  •  忘掉有多难
    2021-01-03 12:36

    I designed this string for matching in regex in my assignment:

    (_?[a-zA-Z0-9_]+)(\s+)(([a-zA-Z]?_[a-zA-Z])?([a-zA-Z]*[0-9]*_*)(=[a-zA-Z0-9]*)?[,]?)+((\s+)(([a-zA-Z]?_[a-zA-Z])?([a-zA-Z]*[0-9]*_*)(=[a-zA-Z0-9]*)?[,]?))*
    

    It matches all declarations including using namespace std. So, you need to remove keywords before checking group1 for datatype. If the datatype is valid, you can remove group1 string, and will be left with only comma separated variables, including ones with assignment operators.

    The following code is matched properly too:

    int a=3, b=9, c, N

    Even int i=0 is matched in a for loop: for(int i=0; i

    This regex string does require you to do more work in filtering (like removing keywords before checking) but in turn, matches in cases where other regex strings fail. EDIT: Forgot to mention that it detects _ and all combinations of alphanumeric declarations too.

    EDIT2: Slight modification to regex string:

    ([a-zA-Z0-9_]+)(\\s+)(([a-zA-Z_\\*]?[a-zA-Z0-9_]*(=[a-zA-Z0-9]+)?)[,;]?((\\s*)[a-zA-Z_\\*]?[a-zA-Z0-9_]*?(=[a-zA-Z0-9]+)?[,;])*)
    

    This matches all variable and method declarations. So, all you need to do is check if reg_match->str(1) is a datatype or not. If yes, you can use sregex_token_iterator (with a regex separator of (\\s*)[,.;](\\s*)) on reg_match->str(3) to get all user defined identifiers.

提交回复
热议问题