XML取值問題

看板RegExp (正規表示式 Regular Expression)作者 (adios)時間1年前 (2022/09/06 20:27), 編輯推噓0(005)
留言5則, 2人參與, 1年前最新討論串1/1
想問一下 手邊有個xml 架構大概是這樣.. 想請問一下 該怎麼下才能取得<TOTA>裡length的值?? <TITA> <TT>(其他外層的tag) <TxField id="AVG03" cname="hh" datatype="9" lengthtype="F" padchar=" " justify="" default="" length="13" lengthExpr="" scale="2" tagSize="0" lengthSize="0" encoding="" shiftInOut="Y" invisibleChar="TrimAndPadRight" memo="" optional="N" overwrite="N" codec="" renderTag="" charFormat="" /> <TxField id="AVG03" cname="hh" datatype="9" lengthtype="F" padchar=" " justify="" default="" length="13" lengthExpr="" scale="2" tagSize="0" lengthSize="0" encoding="" shiftInOut="Y" invisibleChar="TrimAndPadRight" memo="" optional="N" overwrite="N" codec="" renderTag="" charFormat="" /> ..... </TT> </TITA> <TOTA> <TT>(其他外層的tag) <TxField id="AVG03" cname="hh" datatype="9" lengthtype="F" padchar=" " justify="" default="" length="13" lengthExpr="" scale="2" tagSize="0" lengthSize="0" encoding="" shiftInOut="Y" invisibleChar="TrimAndPadRight" memo="" optional="N" overwrite="N" codec="" renderTag="" charFormat="" /> <TxField id="AVG03" cname="hh" datatype="9" lengthtype="F" padchar=" " justify="" default="" length="13" lengthExpr="" scale="2" tagSize="0" lengthSize="0" encoding="" shiftInOut="Y" invisibleChar="TrimAndPadRight" memo="" optional="N" overwrite="N" codec="" renderTag="" charFormat="" /> ..... </TT> </TOTA> -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.137.17.234 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/RegExp/M.1662467259.A.D60.html

09/06 20:54, 1年前 , 1F
/(?<=<TOTA>[^]+\blength=")[^"]+(?="[^]+<\/TOTA>)/g
09/06 20:54, 1F

09/06 20:57, 1年前 , 2F
上面適用於你目前的內容,但若有多組<TOTA>會有問題
09/06 20:57, 2F

09/06 21:00, 1年前 , 3F
這種還是直接用XML相關解析工具會比較穩健
09/06 21:00, 3F

09/07 08:03, 1年前 , 4F
謝謝!不過在regex101測試好像不行?
09/07 08:03, 4F

09/07 19:17, 1年前 , 5F
我用ECMAScript語法,regex101的話要在左側欄自己切換
09/07 19:17, 5F
文章代碼(AID): #1Z5poxrW (RegExp)
文章代碼(AID): #1Z5poxrW (RegExp)