首页 > 解决方案 > REGEX - 自动文本选择和重组

问题描述

我对 AHK 有点陌生,我写了一些脚本。但是对于我最新的脚本,我有点坚持使用 AHK 中的 REGEX。我想报告我所做的文本结构。

为此,我建立了一个系统:

  1. 以“.”结尾的句子是带有“-”的重要句子。(变量“Vimportant”)但没有提到“Vanecdotes2”或“Vdelete2”cfr 的词。4
  2. 以“.*”结尾的句子是轶事(变量“Vanecdotes1”),我在该点之后手动放置了一个星号。
  3. 以“.!”结尾的句子是不相关的句子,需要删除(变量“Vdelete1”)如果我在该点之后手动放置了一个星号。
  4. 我要实现的一个额外选项是要在句子中检测的单词,以便将句子自动添加到变量“Vanecdotes2”或“Vdelete2”中

一个随机的例子是这样的(我已经在句子后面加上了 ! 和 * (为什么不重要),其中“获取”是我上面第 4 点的一个例子 op Vanecdotes2):

最后一次程序于 2019 年 8 月 19 日。

Normal structure x1.!  
Normal structure x2.!  
Abberant structure x3, needs follow-up within 2 months.  
Structure x4 is lower in activity, but still above p25.  
Abberant structure x4, needs follow-up within 6 weeks.  
Normal structure x5.  
Good aqcuisition of x6.  

所以变量中正则表达式的输出应该是

Last procedure on 19/8/2019.  
Normal structure x1.! --> regex  '.!' --> Vdelete1  
Normal structure x2.! --> regex  '.!' --> Vdelete1  
Abberant structure x3, needs follow-up within 2 months. --> Regex '.' = Vimportant  
Structure x4 is lower in activity, but still above p25.* --> regex '.*' = Vanecdote1  
Abberant structure x4, needs follow-up within 6 weeks. --> Regex '.' = Vimportant  
Normal structure x5.! --> regex  '.!' --> Vdelete1  
Good aqcuisition of x6. --> Regex 'sentence with the word acquisition' = Vanecdote2  

输出应该是:

'- Last procedure on 19/8/2019.  
 - Abberant structure x3, needs follow-up within 2 months.  
 - Abberant structure x4, needs follow-up within 6 weeks.  

. Structure x4 is lower inactivity, but still above p25.  
. Good aqcuisition of x6.

但是我在使用正则表达式时遇到了很多麻烦,特别是在选择以 * 或 ! 结尾的句子时。但也有排除标准,他们只是不想这样做。

因为 AHT 没有真正好的测试器,所以我首先在另一个正则表达式测试器中对其进行了测试,我打算稍后将其“翻译”为 AHK 代码..但它就是行不通。(所以我知道在下面的脚本中我使用 AHK 语言和非 AHK 正则表达式,但我只是将 to 放在一起以进行说明)

这就是我现在拥有的:

Send ^c  
clipwait, 1000  
Temp := Clipboard  
Regexmatch(Temp, "^.*[.]\n(?!^.*\(Anecdoteword1|Anecdoteword2|deletewordX|deletewordY)\b.*$)", Vimportant)  
Regexmatch(Temp, "^.*[.][*]\n")", Vanecdotes1) 
Regexmatch(Temp, "^.*[.][!]\n")", Vdelete1)   
Regexmatch(Temp, "^.*\b(Anecdoteword1|Anecdoteword2)\b.*$")", Vanecdotes2)  
Regexmatch(Temp, "^.*\b(deletewordX|deletewordY)\b.*$")", Vdelete2)   
Vanecdotes_tot := Vanecdotes1 . Vanecdotes2  
Vdelete_tot := Vdelete1 . Vdelete2  
Vanecdotes_ster := "* " . StrReplace(Vanecdotes_tot, "`r`n", "`r`n* ")  
Vimportant_stripe := "- " . StrReplace(Vimportant, "`r`n", "`r`n- ")  
Vresult := Vimportant_stripe . "`n`n" . Vanecdotes_ster  

对于“翻译为 AHK”,我尝试^.*\*'n使用工作(非 ahk)正则表达式^.*[.][*]\n

标签: regexreportautohotkey

解决方案


没有真正的 AHK 正则表达式。AHK 几乎使用 PCRE,除了options
所以不要试图将换行符\n变成AHK 换行符 `n

您的正则表达式中似乎存在一些语法错误。不太确定那些额外")的东西应该是什么。此外,[.][*]应该使用\.\*. 这些\特定字符需要 来逃避其正常功能(任何字符以及零和无限之间的匹配)。
[]是匹配该组中的任何字符,例如如果你想匹配.或者*你会匹配[.*].

似乎您有了使用捕获组的想法,但以防万一,这里有一个关于它们的最小示例:

RegexMatch("TestTest1233334Test", "(\d+)", capture)
MsgBox, % capture

最后,关于您解决问题的方法,我建议逐行循环输入。它会更好/更容易。使用例如LoopParse
它的最小示例:

inp := "
(
this is
a multiline
textblock
we're going
to loop
through it
line by line
)"

Loop, Parse, inp, `n, `r
    MsgBox, % "Line " A_Index ":`n" A_LoopField

希望这有帮助。


推荐阅读