【问题标题】:How to find a string not containing specific word in x-lines如何在 x 行中查找不包含特定单词的字符串
【发布时间】:2023-01-03 02:33:41
【问题描述】:

我有很多 json 文件和内部标签,比如

actions:
    - name: email
      template: email_exp_csr_008
      schedule:
       week: false
       workdays: true
       persistent: true

我需要在接下来的 3 行中找到“schedule:”未跟在“workdays:”之后的情况。上面的例子不应该匹配,但这个应该:

actions:
    - name: email
      template: email_exp_csr_008
      schedule:
       week: false
       persistent: true
       werwer: True

我这样试过

schedule:(.*\n){0,3}.*[^(workdays)]

但它并不能解决问题。有人可以帮忙吗?

【问题讨论】:

  • 我删除了 JS 标签并添加了 yaml。如有需要请重新标记
  • 请指定您要使用哪种语言通过正则表达式解析 YAML(正则表达式风格因语言/引擎而异)。根据语言的不同,我们也可能会建议更好的工具来解决您的任务。

标签: regex yaml


【解决方案1】:

例如,您可以使用否定前瞻来查找时间表:在接下来的几行中,通过使用捕获组和反向引用至少以相同数量的缩进开始:

^([^S
]*)schedule:(?!
[^S
]*w+:.*
[^S
]*workdays:)

regex101 demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    相关资源
    最近更新 更多