我有一个类似table {
color: #212121;
font-size: .875rem;
margin: 1.25rem 0;
border-collapse: collapse;
table-layout: fixed;
width: 100%;
border: 1px solid red;
}
tr {
display:flex;
justify-content: center;
text-align:center;
}
tr td{
border: 1px solid blue;
width:50%;
}
tr td span{
display:inline-block;
width:50%;
text-align:left;
white-space:break;
}
的字符串,我想要获得函数名称“ str”和参数名称“ * p”,“ a”。但是,我不知道有多少个参数。
我已经为正则表达式写了String str = "void Write(int *p,int a)"
。
第1部分= "int\\s+|void\\s+|string\\s+|float\\s+|double\\s+|char\\s+\\(,\\)"
第2部分= Write(
第3部分= *p,
正则表达式a)
的最后一部分是删除分号和括号。但是如您所见,它失败了。我必须使用第二个拆分还是有其他方法吗?
答案 0 :(得分:0)
这将是一个两步过程
第1步:提取功能名称和所有参数
第2步:从所有参数列表中提取每个参数名称
步骤1:
让此正则表达式^\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
第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
希望有帮助