正则要找到一个在括号内没有再有两个单词的单词
我正在尝试将规则添加到代码覆盖中,这将要求我所有功能都具有指定的输入参数类型。 这些是我可以编译的一些不同的选项:
function someName()
function someName(someParam)
function someName(someParam as int)
function someName(someParam = "" as int)
function someName(someParam = "")
function someName(someParam, otherParam)
function someName(someParam as int, otherParam)
function someName(someParam, otherParam as int)
function someName(someParam as int, otherParam as int)
匹配为无效的:
function someName(someParam)
function someName(someParam = "")
function someName(someParam, otherParam)
function someName(someParam as int, otherParam)
function someName(someParam, otherParam as int)
因此,我希望所有输入参数都不具有as as< lt;
但是我想将它们 这些示例,我可以使用:
function \w+\(.*Param(?! (= .*|)as \w+).*\)
但是我无法弄清楚如何使其与任何输入参数名称一起使用,
我可以通过多个通行证可以匹配不同的无效案例,只要它们与有效的情况不匹配
I am attempting to add a rule to code linting that would require all of my functions to have a input parameters type specified.
These are some different options I am able to compile:
function someName()
function someName(someParam)
function someName(someParam as int)
function someName(someParam = "" as int)
function someName(someParam = "")
function someName(someParam, otherParam)
function someName(someParam as int, otherParam)
function someName(someParam, otherParam as int)
function someName(someParam as int, otherParam as int)
But I want to match these as invalid:
function someName(someParam)
function someName(someParam = "")
function someName(someParam, otherParam)
function someName(someParam as int, otherParam)
function someName(someParam, otherParam as int)
So, I want all cases where any of input parameters not have as <some text>
to be matched
In these examples, I can use:
function \w+\(.*Param(?! (= .*|)as \w+).*\)
but I cannot figure out how to make it work with any input parameter name
I am fine with multiple passes to match different invalid cases, as long as they do not match valid ones
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用此正则罚款来查找所有有效的匹配:
regex demo
任何不匹配的线被认为是无效的匹配。
REGEX详细信息:
函数\ s+\ w+
(
(?:
:start 可选非捕捉组\ s*\ w+\ s+as \ s+\ w+
:匹配必须具有关联类型的第一个参数(?:\ s*,\ s*\ w+\ s+as \ s+\ w+)*
:匹配逗号,然后是另一个必须具有关联类型的参数。重复此组0或更多次以匹配所有此类参数)?
:end 可选非捕捉组\ s*
:可选的whitespaces\)
:匹配关闭)
如果只想找到无效的匹配项,则使用:
Regex Demo 2
You can use this regex to find all valid matches:
RegEx Demo
Any line not matching above regex will be considered an invalid match.
RegEx Details:
function\s+\w+
: Matchfunction
followed by name of the function\(
: Match starting(
(?:
: Start optional non-capture group\s*\w+\s+as\s+\w+
: Match first parameter that must have associated type(?:\s*,\s*\w+\s+as\s+\w+)*
: Match comma followed by another parameter that must have associated type. Repeat this group 0 or more times to match all such parameters)?
: End optional non-capture group\s*
: optional whitespaces\)
: Match closing)
If you only want to find invalid matches then use:
RegEx Demo 2