【问题标题】:REGEX - Automatic text selection and restructeringREGEX - 自动文本选择和重组
【发布时间】:2020-06-28 17:10:13
【问题描述】:

我对 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.  

所以变量中Regex的输出应该是

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”,我尝试从工作(非 ahk)正则表达式 ^.*[.][*]\n 制作 ^.*\*'n

【问题讨论】:

    标签: regex report autohotkey


    【解决方案1】:

    实际上并没有 AHK 正则表达式这样的东西。 AHK 几乎使用 PCRE,除了 options.
    所以不要试图将换行符 \n 变成 AHK 换行符 `n

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

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

    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
    

    希望这对您有所帮助。

    【讨论】:

    • 对不起,我在复制粘贴时犯了一些错误(“”)来自复制粘贴)。我真的不明白您所说的“所以不要尝试换行 \n到 AHK 换行 `n"。我可以在 AHK 中将 \n 用于正则表达式?那么两者之间有什么区别?我一直在研究我的正则表达式,现在他们在测试器上工作。(?=.*[. >
    【解决方案2】:

    这是我到现在为止,没有任何工作(当正则表达式工作时,我会尝试建议的循环):^m:: 块输入,打开 MouseGetPos, , ,TempID, 控制 WinActivate, ahk_id %TempID% 如果 WinActive("Pt.") 发送 ^c 剪辑等待,1000 温度:=剪贴板 Regexmatch(Temp, "(^(?:..\n)((?! PAX|PAC|Normaal|Geen).)$)", Vimportant) Vimportant := Vimportant.1 正则表达式匹配(温度,“(^..*\n)”,Vanecdotes1_ster) 正则表达式匹配(温度,“(^..!\n)”,Vdelete1_uitroep) Regexmatch(Temp, "(^.\b(PAX|PAC)\b.$)", Vanecdotes2) 正则表达式匹配(温度,“(^.\b(Normaal|Geen)\b.$)”,Vdelete2) Vanecdotes1 := StrReplace(Vanecdotes1_ster, ".", ".") Vdelete1 := StrReplace(Vdelete1_uitroep, ".!", ".") Vanecdotes_tot := Vanecdotes1 。传闻2 Vdelete_tot := Vdelete1 。删除2 Vanecdotes_ster := "" 。 StrReplace(Vanecdotes_tot,“rn”,“rn*”) Vimportant_stripe := "-" 。 StrReplace(Vimportant, "rn", "rn-") Vresult := Vimportant_stripe 。 “nn”。 Vanecdotes_ster 剪贴板 := Vresult 发送^v 返回

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-18
      相关资源
      最近更新 更多