有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用单词和符号进行正则表达式Java拆分

我有一个类似String str = "void Write(int *p,int a)"的字符串,我想得到函数名“str”和参数名“*p”,“a”。然而,我不知道有多少参数

我已经为regex写了"int\\s+|void\\s+|string\\s+|float\\s+|double\\s+|char\\s+\\(,\\)"

第一部分=Write(第二部分=*p,第三部分=a)

regex \\(,\\)的最后一部分用于删除分号和括号。但正如你所见,它失败了。我必须使用第二次拆分还是有其他方法


共 (1) 个答案

  1. # 1 楼答案

    这将是一个两步的过程

    步骤1:提取函数名和所有参数

    步骤2:从所有参数列表中提取每个参数名称

    第一步:

    让我们将这个正则表达式^\S+\s+([^(]+)\(([^)]+)*应用于这个字符串void Write(int *p,int a, int b, str *v)这个测试字符串

    ^         # start of string
    \S+       # one or more occurence of any non space charactcers
              # matches `void`
    \s+       # one or more occurence of a space character
              # matches the space after `void`
    ([^(]+)   # all characters until opening parenthesis
              # matches `Write` and capture it
    \(        # literally matches opening parenthesis
    ([^)]+)   # matches all characters till closing parenthesis is encountered
              # matches arguments signature i.e. `int *p,int a, int b, str *v`
    *         # matches zero or more occurrence of last capturing group
              # last capturing group is string between the parenthesis
              # so this star handle the corner case when the argument list is empty
    

    更多详细信息:https://regex101.com/r/0m1vs9/2

    第二步

    现在对参数列表(int *p,int a, int b, str *v)应用这个带有全局修饰符的正则表达式\s*\S+\s+([^,]+),?

    该模式匹配逗号之间的文本,以便解释假设相同的模式

    \s*      # matches zero or more occurrences of a space character
             # this will match any spaces after comma e.g. `int b,<space> str`
    \S+      # one or more occurrence of non space character
             # matches argument type, i.e. `int`
    \s+      # one or more occurrence of space characters
             # matches the space between argument name and type, e.g. `int<space>b`
    ([^,]+)  # capture all characters till comma
             # this matches the actual argument name
             # and also matches any spaces after it
    ,?       # zero or one occurrence of a comma
             # this ensures that the argument name is immediately followed by a comma
             # this also handles the case for the last argument which doesn't have any comma after it
    

    更多详细信息:https://regex101.com/r/9ju60l/1

    希望有帮助