【问题标题】:Autohotkey Regex Lookbehind AssertionAutohotkey 正则表达式 Lookbehind 断言
【发布时间】:2015-03-08 05:22:22
【问题描述】:

在我的脚本中,'matchfile 是一个带或不带扩展名的文件名。

在我下面的文字中,可能有也可能没有[AnyText],括号之间的文字可以是任何字母数字字符。

如何匹配:

some text
\includescore{something}
\includescore{something.ext}
%\includescore{doNOT.match}
\includescore[AnyText]{something}
\includescore[AnyText]{something.ext}
more text

到目前为止,我有这个:

regexmatch(searchthis, "mi)(?<=(?<!%)\\includescore\{|(?<!%)\\includescore\[\w\]\{)[\w\s\.\+'#-]+\.?[\w\s\.\+'#-]+(?=.*\})", matchfile)

但那只能匹配:

\includescore{something}
\includescore{something.extension}
\includescore[a]{something}

如果该行以% 开头,我不想匹配。

我的第一个想法是使用 w+,但 Autohotkey 不允许未量化的后视。

实际上,我什至不关心匹配方括号。我只需要匹配\includescore、matchfile 和大括号。

还有其他方法可以做到这一点,还是我需要进行多次正则表达式调用?

【问题讨论】:

  • includescore 在您的正则表达式中来自哪里?请提供一些真实的输入和预期的输出。

标签: regex autohotkey


【解决方案1】:

也许,这就是你想要的:

mi)^(?:(?&lt;!%)\\includescore(?:\[\w*\])?(?:\{[\w.]*\})|.*matchfile(?:\.\w*)?)

\includescore(?:\[\w*\])?(?:\{[\w.]*\}) 将捕获所有包含可选的[AnyText] 和强制性的{something}。

如果 'matchfile' 不在行首,我添加了.*,可选扩展名将被捕获为(?:\.\w*)?)

【讨论】:

  • 我不能在正则表达式中使用“matchfile”,因为它是一个 ahk 变量。我已经匹配了其中包含的文件名的 pattern。
  • 如果您提供包含匹配文件的此类文本示例,我将更新答案。
  • 对不起,我不认为你理解; matchfile 可以是我的示例中的任何内容:something 或 something.ext。没关系;我想我知道该怎么做了。 +1 非常愿意提供帮助。
【解决方案2】:

我得到了它的工作:

;Fetch the uncommented inclusion line
regexmatch(searchthis, "mi)(?<=(?<!%)\\includescore).*?{(.*?)}", matchfile)

;Now pull out the bracketed item
regexmatch(matchinclusion, "i)(?:\[.*\])", includeparam)        ;get the parameter

;now pull out the filename with/without the extension
regexmatch(matchinclusion, "i)(?<={)(.*)(?=})", inclusionfile)  ;get the tex name

它在所有计数上都匹配,并且不包括用 % 注释的行。

在 Autohotkey 中,它匹配每个实例,并且只返回第一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-08
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 2013-02-23
    相关资源
    最近更新 更多