【问题标题】:AHK: Remove Text From String This Word To The First Line BreakAHK:从字符串中删除文本这个词到第一个换行符
【发布时间】:2017-04-15 09:25:49
【问题描述】:

我在一家医生办公室工作,负责结算,但遇到了问题。由于 EMR(电子病历)程序的工作方式存在问题,免疫代码并不总是出现在图表中。为了弥补这一点,我让 AHK 也搜索了免疫的名称(危​​险,我知道)。直到最近,这一切都很好,直到其中一名医生取消了免疫订单。这引发了误报,因为我为没有代码的图表(IMM94 和 IMM97)包含了故障保护。我需要从一个非常大的字符串中删除单词“CANCELED:”之后的所有内容,直到该单词之后出现的第一个换行符。我创建了一个相当漂亮的字符串剪辑函数来完成大部分此类工作,但是由于太多了,所以在查找换行符时遇到了问题。我正在使用的测试用例是:

评估和计划:

  1. 需要预防性疫苗接种和流感疫苗接种(选项卡)已取消:流感,高剂量 (65+)(换行)
  2. 糖尿病前期致力于减肥和锻炼
  3. 骨质减少运动和 vit d 和 b 12

上面和下面都有很多东西,但我想从我的单词“CANCELED:”字符串中提取所有实例到它之后的第一个换行符。

这是我正在尝试的,以及我的功能。它不起作用,实际上,它正在复制以“1.”开头的行:

^L::
lString := ClipBoard
If lString Contains Canceled
fString := ClipString(lString, "CANCELED: ", "`n")
Clipboard := fString
Return


ClipString(lString, aMarker, bMarker, Only := 0)
{ 
If (Only = 1)
Return SubStr(SubStr(lString, 1, InStr(lString, bMarker)+StrLen(bMarker)-1), InStr(lString, aMarker))
Else
Return SubStr(lString, 1, InStr(lString, aMarker)-StrLen(aMarker)) . SubStr(lString, InStr(lString, bMarker)+StrLen(bMarker))
}

【问题讨论】:

    标签: string search clipboard autohotkey


    【解决方案1】:

    从剪贴板中的每一行删除“已取消:”及其后面的任何文本

    计划:

    Clipboard := RegExReplace(Clipboard, "im)\s*CANCELLED:.*\R?", "`r`n")
    

    示例输入:

    alpha
    beta cancelled: ABC asldkfalsd 
    delta
    gamma omega CANCELLed: zeta
    theta
    

    样本输出:

    alpha
    beta 
    delta
    gamma omega 
    theta
    

    【讨论】:

    • 您可能希望根据操作系统的默认行尾字符序列将`r`n 更改为`r(例如,`r`n 用于 windows,`n 用于 *nix)
    • 请不要发布裸代码,同时解释代码的作用。
    • @JonathanMee 我的代码被评论了。反正我的回答是。 (我认为评论损坏的代码并没有太大帮助,因为我在问题中描述了我试图做的事情。)
    • @DarknessCalling 我的评论是在 Jim U 的裸代码答案中解决的。你似乎以一种让我害怕你是Legion的方式声称Jim U的代码
    • 我在正则表达式中添加了?,因此即使没有行尾字符,剪贴板的最后一行也会被处理。 \s* 添加到任何匹配项之前的去除空格
    【解决方案2】:

    想通了。我会评论它以帮助遇到此问题的任何人。我喜欢你通常可以获得功能的紧凑程度,但这件作品确实需要比我想要的多几行。如果有人能把它降低到更少,并且仍然可以工作,请告诉我!

    ^L::
    ClipBoard := ClipString(ClipBoard, "Canceled: ", "`n",2) ;This is the function call here.
    ;The first parameter is the string to do stuff to, the second is what to cut out of the 
    ;string, and the third parameter is how far after that piece to cut, so if you wrote 
    ;Canceled: thenawholebunchofstuffthena newline, it'd remove all of it except for the 
    ;newline. The third parameter is which mode to put ClipString to.
    Return
    
    
    ClipString(lString, aMarker, bMarker, Mode := 0) ;already explained above. 
    ;Mode defaults to zero because I call the zero mode a lot.
    { 
    If (Mode = 0) ;Mode Zero returns the ONLY the section from the original string 
    ;between aMarker and bMarker.
        Return SubStr(lString, 1, InStr(lString, aMarker)-StrLen(aMarker)) . SubStr(lString, InStr(lString, bMarker)+StrLen(bMarker))
    Else If (Mode = 1) ;Mode One returns the original string with the section between aMarker 
    ;and bMarker removed.
        Return SubStr(SubStr(lString, 1, InStr(lString, bMarker)+StrLen(bMarker)-1), InStr(lString, aMarker))
    Else If (Mode = 2) ;Mode Two returns the original string with all instances of aMarker 
    ;to bMarker removed. I.E. Every occurrence of aMarker will be removed to bMarker.
        {
        aString := lString
        StringReplace, aString, aString, %aMarker%, %aMarker%, UseErrorLevel ;Count how many 
    ;instances of aMarker are in the original string.
        CanCnt := Errorlevel ;actual count of # of aMarkers
        Loop, %CanCnt% ;loop as many times as there are aMarkers
            {
            aString := SubStr(aString, 1, InStr(aString, aMarker)-1) . SubStr(SubStr(aString, InStr(aString, aMarker)), InStr(SubStr(aString, InStr(aString, aMarker)), bMarker)-1) 
    ;this is a tad complicated. The first part before the concatenate takes the string, and
    ;keeps from the start point to the first aMarker. The concatenate adds the substring of the 
    ;substring between aMarker and the end of the string, and the location of the substring in 
    ;aString from the occurrence of aMarker to one position before the start of bMarker. 
    ;An easier way to read this would be to replace "SubStr(aString, InStr(aString, aMarker)) 
    ;with a variable like bString. It shortens it up quite a bit.
            }
        Return aString
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      • 2022-01-16
      相关资源
      最近更新 更多