【问题标题】:Regex to find X words only when a word doesn't exist正则表达式仅在单词不存在时查找 X 单词
【发布时间】:2019-01-18 20:47:44
【问题描述】:

我正在尝试做一些代码重构,因为我做了很多工作,我正在使用 WebStorm 的正则表达式 find in files 来查看哪些文件仍需要重构。

我知道这个(?:^|(?=[^']).\b)(this.user|this.isVatRegistered|showStatsInNet)\b 将显示所有包含这些代码位之一的文件。

根据这篇文章:Match string that doesn't contain a specific word^(?!.*UserMixin).*$ 应该做一个否定的前瞻,只有当那个词不存在时才匹配。

我的问题是我不知道如何组合它们。有人可以提供一些指导吗?

我尝试过像这样组合:(?:^|(?=[^']).\b)(?!.*UserMixin)(this.user|this.isVatRegistered|showStatsInNet)\b 但无济于事。

TL;DR如何仅在不存在另一个单词时匹配 X 个单词?

【问题讨论】:

  • 像这样组合它们可以工作 - (?:^|(?=[^']).\b)(?!.*UserMixin)(this.user|this.isVatRegistered|showStatsInNet)\b.
  • @Kamehameha 我在发布之前尝试过,但它不起作用。抱歉,我应该提到这一点。
  • 试试\A(?![\d\D]*?UserMixin)[\d\D]*?\b(?:this\.(?:user|isVatRegistered)|showStatsInNet)\b
  • 那行得通。谢谢你。请发布答案,我会接受。

标签: regex webstorm


【解决方案1】:

您必须使用负前瞻,然后尝试匹配这些子字符串:

\A(?![\d\D]*?UserMixin)[\d\D]*?\b(?:this\.(?:user|isVatRegistered)|showStatsInNet)\b

这会很耗时,因为有两个 [\d\D]*? 出现会逐个字符地将光标移动到文件内容的末尾。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    相关资源
    最近更新 更多