正则要找到一个在括号内没有再有两个单词的单词

发布于 2025-02-12 01:22:04 字数 905 浏览 1 评论 0原文

我正在尝试将规则添加到代码覆盖中,这将要求我所有功能都具有指定的输入参数类型。 这些是我可以编译的一些不同的选项:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷了相思 2025-02-19 01:22:04

您可以使用此正则罚款来查找所有有效的匹配:

function\s+\w+\((?:\s*\w+\s+as\s+\w+(?:\s*,\s*\w+\s+as\s+\w+)*)?\s*\)

regex demo

任何不匹配的线被认为是无效的匹配。

REGEX详细信息:

  • 函数\ s+\ w+
  • : MATT
  • (?::start 可选非捕捉组
    • \ s*\ w+\ s+as \ s+\ w+:匹配必须具有关联类型的第一个参数
    • (?:\ s*,\ s*\ w+\ s+as \ s+\ w+)*:匹配逗号,然后是另一个必须具有关联类型的参数。重复此组0或更多次以匹配所有此类参数
  • )?:end 可选非捕捉组
  • \ s*:可选的whitespaces
  • \):匹配关闭

如果只想找到无效的匹配项,则使用:

function\s+\w+\((?!(?:\s*\w+\s+(.*?)as\s+\w+(?:\s*,\s*\w+\s+(.*?)as\s+\w+)*)?\s*\))

Regex Demo 2

You can use this regex to find all valid matches:

function\s+\w+\((?:\s*\w+\s+as\s+\w+(?:\s*,\s*\w+\s+as\s+\w+)*)?\s*\)

RegEx Demo

Any line not matching above regex will be considered an invalid match.

RegEx Details:

  • function\s+\w+: Match function 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:

function\s+\w+\((?!(?:\s*\w+\s+(.*?)as\s+\w+(?:\s*,\s*\w+\s+(.*?)as\s+\w+)*)?\s*\))

RegEx Demo 2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文