AutoHotkey:搜索 3 个特定字符,然后为该列中的每个有效实例从右边搜索 trim 8 个字符

AutoHotkey: Search for 3 specific characters then trim 8 characters from right for each valid instance in that column

如果我无法用通俗易懂的方式解释这一点,我深表歉意。

我有的是: 从中获取数据的 syslistview。 Column1数据需要修改

我们知道的是: 我们要查找的 3 个字符将始终位于第 1 列文本的最右边,如果它们存在的话。(这 3 个字符是 "FUT")

需要的是: 如果在 column1 row1 中找到 "FUT",则从右起 TRIM 8 个字符。 (也包括 "FUT" 的修剪)。然后向下移动并重复直到该列的最后一个值。

以下是我到目前为止开发的代码。如果找到 "FUT",需要帮助移除 "xxxxxFUT":

 #Persistent
 #Include %A_ScriptDir%
 #SingleInstance force
 #NoEnv
 SetWorkingDir %A_ScriptDir%
 SetTitleMatchMode, 1
 SetKeyDelay, 150, 150, Play
 SetControlDelay,0
 iffut:="FUT"
 SetTimer,datos,10
 return

 datos:
 IfWinExist,DataTable ahk_class #32770
  {
    WinGetTitle,winname,DataTable   
    StringTrimLeft,winname,winname,11
    IfInString,winname,%iffut%
      {
        StringTrimRight,winname,winname,8
      }

    ControlGet,dato,List,,SysListView321,DataTable

    StringReplace, dato, dato, %A_Tab%, `,, All   
    StringReplace, dato, dato, %A_Space%, `,, All 
    IfInString,dato,%iffut%
      {
        ;MsgBox,there
         StringReplace, dato, dato, .{5}FUT, , All
      }

    FormatTime, mydttm, , ddMMyy_HHmmss
    FileAppend,%dato%,%winname%_%mydttm%.txt

    return
  }

在这种情况下,我将使用正则表达式 match "FUT" and than SubStr() 到 trim 出 8 个字符。

下面是一个例子:

var = any amount of data can be here and xxxxxFUT as you can see FUT is removed!
; ~= is shorthand for RegExMatach 
MsgBox % SubStr(var, 1, (var ~= "FUT") - 6) SubStr(var, (var ~= "FUT") + 3)

编辑:上面对代码进行了编辑以更好地反映您的问题,因为您对其进行了扩展。

现在将其应用到您的代码中:

 #Persistent
 #Include %A_ScriptDir%
 #SingleInstance force
 #NoEnv
 SetWorkingDir %A_ScriptDir%
 SetTitleMatchMode, 1
 SetKeyDelay, 150, 150, Play
 SetControlDelay,0
 iffut:="FUT"
 SetTimer,datos,10
 return

 datos:
 IfWinExist,DataTable ahk_class #32770
  {
    WinGetTitle,winname,DataTable   
    StringTrimLeft,winname,winname,11
    IfInString,winname,%iffut%
      {
        StringTrimRight,winname,winname,8
      }

    ControlGet,dato,List,,SysListView321,DataTable

    StringReplace, dato, dato, %A_Tab%, `,, All   
    StringReplace, dato, dato, %A_Space%, `,, All 
    Loop, parse, dato, `n, `r 
    {
        IfInString, A_LoopField, %iffut%
            Results .= SubStr(A_LoopField, 1, (A_LoopField ~= "FUT") - 6) SubStr(A_LoopField, (A_LoopField ~= "FUT") + 3) "`n"
        Else 
            Results .= A_LoopField "`n"
    }
    FormatTime, mydttm, , ddMMyy_HHmmss
    FileAppend,%Results%,%winname%_%mydttm%.txt

    return
  }

RegExReplace 是最快最简单的方法:

str := "before15NOVFUTsomeotherstring"
newStr := RegExReplace(str, ".{5}FUT", "")
msgbox % newStr

该模式匹配 "FUT" 以及后面的任意 5 个字符,并将它们替换为空白字符串。如果没有找到,它什么也不做。

编辑:

更好的答案是

  dato := RegExReplace(dato, ".{5}FUT", ""). 

不需要检查它是否在字符串中,因为如果不在,它什么也不做。 – 由 --> Elliot DeNolf

提供

在其他地方得到了答案,所以也张贴在这里。

创建字符串模式,然后使用 regexreplace 删除所有此类字符串。

下面是实现的代码:

 regex_patten = \d{2}[a-zA-Z]{3}FUT

 if RegExMatch(string, regex_patten)
 newstring := RegExReplace(string, regex_patten, "")

 msgbox % "old string:`n`n" string "`n`nnew string`n`n" newstring