xStr H, C, B ,E, BO, EO, BI, EI, BT, ET )

参数名称扩展
H = Haystack
C = 区分大小写(对于 B,E)
B = 开始匹配
E = 结束匹配
BO = 开始偏移
EO = 结束偏移
BI = 开始实例
EI = 结束实例
BT = 开始(未)修剪
ET = 结束(未)修剪


xStr() 能做什么?

xStr()是SubStr()的包装器。它主要是为了解析 XML、HTML 等结构化文本而编写的。可以使用 带有起始位置和长度参数的 SubStr()
从文本中提取子字符串。

MsgBox % SubStr("This is <b>BOLD</b> text", 12, 4) ; Returns "BOLD"

 

由于子字符串“BOLD”位于 HTML 标记对 <b> 和 </b> 之间,因此我们可以使用两个Instr() 调用动态提取它。

H   := "This is <b>BOLD</b> text"  ; Haystack
B   := "<b>"                       ; Begin match
E   := "</b>"                      ; End match
P1  := InStr(H, B)                 ; Position of Begin match 
P2  := InStr(H, E)                 ; Position of End match
Res := SubStr(H, P1:=P1+StrLen(B), P2-P1)

MsgBox % Res

 

xStr()可以同时执行这两个操作

H := "This is <b>BOLD</b> text"  ; Haystack
MsgBox % xStr(H,,,,12,4)         ; Equivalent of SubStr(H,12,4)
MsgBox % xStr(H,,"<b>","</b>")   ; Dynamic extraction

 

更重要的是,您可以混合使用这两种方法......只需记住忽略不需要的参数即可。

 

参数解释

XSTR()需要一个压倒性的10个参数,因为SubStr()需要3,并且instr()需要5,而且XSTR()可以两次调用instr()
来自文档的两个函数的参数,

newstr:= substr(string,startingpos,长度)
uspos:= instr(haystack,neader,casesIntive:= false,startingpos:= 1,出现:= 1)

Xstr()参数为H,C,B,E,BO,EO,BI,EI,BT,ET等

参数H(Haystack)对于两个调用都是常见的。它作为substr()字符串参数传递,作为instr()调用参数 c ( case敏感性)的Haystack参数传递给B 开始匹配)或E(END MATCE)或两者兼而有之。

  • 有效标志为0,1,2,3使用十六进制值以进行可读性。0或0x0(位00)均不是情况敏感(默认值)
    1或0x1(位01)开始匹配是情况敏感的
    2或0x2(位10)最终比赛是案例敏感
    3或0x3(位11)开始/结束匹配是案例敏感的

参数b,e(开始匹配,结束匹配)将传递给相应的instr()调用作为针头参数

  • ERROLEVEL:指定/结束匹配/结束匹配(或两个)指定并且匹配失败时,ErrorLevel设置为1

当参数b(开始匹配)为null或instr()时, 参数bo作为起始 pos to substr()否则

  • BO是BYREF参数。XSTR()将随时更新VAR。
P := 19                  ; Starting position
MsgBox % xStr(H,,,,P,7)  ; Extract 7 chars fron starting position 
MsgBox % P               ; Starting postion has been updated

当参数E(结束匹配)为空时,参数EO作为长度传递 给SubStr(),否则作为起始位置传递给Instr(), 参数BI、EI(开始实例和结束实例)作为出现 参数BT传递给相应的Instr()调用,ET(开始修剪、结束修剪)用于计算结果字符串的最终长度。 正值将修剪结果字符串,负值将取消修剪(扩展)结果字符串。 当省略参数B,E,B0,EO时 ,这些参数将模拟已弃用的命令

StringTrimLeftStringTrimRight

 

更多详细信息和示例:https://www.autohotkey.com/boards/viewtopic.php?t=74050

 

真实示例 #1 - 最新美元汇率(XML 解析)

; 更多详细信息和示例:https://www.autohotkey.com/boards/viewtopic.php?t=74050
#NoEnv
SetBatchLines -1
SetWorkingDir %A_ScriptDir%

URLDownloadToFile, http://www.floatrates.com/daily/usd.xml, usd.xml ; file size < 82 KiB
FileRead, XML, usd.xml

Pos := 1
List := "USD currency rate as on:`n"
List .= xStr(XML, 0, "<pubDate>","</pubDate>",Pos,,,,14) . "`n" ; H,,B,E,BO,,,,BT

While Item := xStr(XML,,"<item>","</item>",Pos,,,,0,0) { ; H,,B,E,BO,,,,BT,ET
  sPos := 1
  , TC := xStr(Item,, "<targetCurrency>","</targetCurrency>",sPos) ; H,,B,E,BO 
  , TN := xStr(Item,, "<targetName>",    "</targetName>",    sPos) ; H,,B,E,BO
  , TN := LTrim(SubStr(TN,1,25))
  , EX := xStr(Item,, "<exchangeRate>",  "</exchangeRate>",  sPos) ; H,,B,E,BO
  , IN := xStr(Item,, "<inverseRate>",   "</inverseRate>",   sPos) ; H,,B,E,BO
  , List .= Format("`n{1:-27}`t1 {2:} = USD {3:}`t`t1 USD = {2:} {4:15}", TN,TC,IN,EX)
}

FileDelete, usd.txt
FileAppend, %List%, usd.txt
Run Notepad usd.txt,,Max
Return


xStr(ByRef H, C:=0, B:="", E:="",ByRef BO:=1, EO:="", BI:=1, EI:=1, BT:="", ET:="") {
  Local L, LB, LE, P1, P2, Q, N:="", F:=0 ; xStr v0.97 by SKAN on D1AL/D343 @ tiny.cc/xstr  
  Return SubStr(H,!(ErrorLevel:=!((P1:=(L:=StrLen(H))?(LB:=StrLen(B))?(F:=InStr(H,B,C&1,BO,BI))?F+(BT=N?LB
    :BT):0:(Q:=(BO=1&&BT>0?BT+1:BO>0?BO:L+BO))>1?Q:1:0)&&(P2:=P1?(LE:=StrLen(E))?(F:=InStr(H,E,C>>1,EO=N?(F
    ?F+LB:P1):EO,EI))?F+LE-(ET=N?LE:ET):0:EO=N?(ET>0?L-ET+1:L+1):P1+EO:0)>=P1))?P1:L+1,(BO:=Min(P2,L+1))-P1)  
}

 

用法示例 #4 - 开始实例并解析 HTML

#NoEnv
#SingleInstance, Force

H := "
(
<!DOCTYPE html>
<html>
<body>
<p><h1>Hello World</h1></p>

<table border='1'>
<tr><td>Table1Row1Col1</td><td>Table1Row1Col2</td><td>Table1Row1Col3</td></tr>
<tr><td>Table1Row2Col1</td><td>Table1Row2Col2</td><td>Table1Row2Col3</td></tr>
<tr><td>Table1Row3Col1</td><td>Table1Row3Col2</td><td>Table1Row3Col3</td></tr>
<tr><td>Table1Row4Col1</td><td>Table1Row4Col2</td><td>Table1Row4Col3</td></tr>
</table>
&nbsp;
<table border='1'>
<tr><td>Table2Row1Col1</td><td>Table2Row1Col2</td><td>Table2Row1Col3</td></tr>
<tr><td>Table2Row2Col1</td><td>Table2Row2Col2</td><td>Table2Row2Col3</td></tr>
<tr><td>Table2Row3Col1</td><td>Table2Row3Col2</td><td>Table2Row3Col3</td></tr>
<tr><td>Table2Row4Col1</td><td>Table2Row4Col2</td><td>Table2Row4Col3</td></tr>
</table>
&nbsp;
<table border='1'>
<tr><td>Table3Row1Col1</td><td>Table3Row1Col2</td><td>Table3Row1Col3</td></tr>
<tr><td>Table3Row2Col1</td><td>Table3Row2Col2</td><td>Table3Row2Col3</td></tr>
<tr><td>Table3Row3Col1</td><td>Table3Row3Col2</td><td>Needle is here</td></tr>
<tr><td>Table3Row4Col1</td><td>Table3Row4Col2</td><td>Table3Row4Col3</td></tr>
</table>
</body>
</html>
)"

;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

; Example 1
; Extract Table 3, Row 3,  Col 3

MsgBox,,Example 1 | Table 3, % T := xStr(H,,"<table ","</table>",,,3,,0,0)   ; H,,B,E,,,BI,,BT,ET
MsgBox,,Example 1 | Row 3,   % T := xStr(T,,"<tr>",   "</tr>",,,   3,,0,0)   ; H,,B,E,,,BI,,BT,ET
MsgBox,,Example 1 | Col 3,   % T := xStr(T,,"<td>",   "</td>",,,   3     )   ; H,,B,E,,,BI

; Example 2
; Extract Table 2 and convert data to CSV

T := xStr(H,,"<table ","</table>",,,2,,0,0) ; H,,B,E,,,BI,,BT,ET

P := 1 ; Starting position
CSV := "", Q := """", C := ",", LF := "`n"

While (TR := xStr(T,,"<tr>","</tr>",P))
{ 
    N := 1 ; Starting position
    CSV .= Q . xStr(TR,,"<td>","</td>",N) . Q . C 
        .  Q . xStr(TR,,"<td>","</td>",N) . Q . C 
        .  Q . xStr(TR,,"<td>","</td>",N) . Q . LF
}         

MsgBox,,Example 2 | CSV, % CSV 

Return 

#Include xStr.ahk

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。