我希望你沒事。
例如,我有一個帶有標簽@tag 的文本,我想將標簽之間的所有文本作為部分獲取(不是一個,請查看最后的圖片,在頂部,結果是 4 個匹配項而不是一個匹配項)。問題出在每個標簽的文本中,可能包含符號@,這將減少我的結果匹配。
我用不同的正則運算式嘗試了幾次,但都沒有成功
我使用的最終正則運算式是:
((^(@))|[^\"]@)[^(@)] \}\n
示例centent文本(我用于嘗試):
@main.xml
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"CENTER"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"CENTER"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}
@main.xml_fab
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"CENTER"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"CENTER"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"@firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"CENTER"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}
@main.xml
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"CENTER"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"CENTER"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}
j
@main.xml_fab
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"CENTER"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"center"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}
{"adSize":"","adUnitId":"","alpha":1.0,"checked":0,"choiceMode":0,"clickable":1,"convert":"","customView":"","dividerHeight":1,"enabled":1,"firstDayOfWeek":1,"id":"_fab","image":{"rotate":0,"scaleType":"CENTER"},"indeterminate":"false","index":0,"inject":"","layout":{"backgroundColor":16777215,"borderColor":-16740915,"gravity"}
這是圖片顯示我的問題

我想獲得@tag1 和@tag2,3,4...之間的整個部分,如果符號@ 在標簽的內容中,我該如何處理它?
uj5u.com熱心網友回復:
我不太確定[^\"]@在你的表達中使用了...它的目的是什么?你能舉個例子嗎?
無論如何,您必須從排除字串列中洗掉 @。并改變您的外觀在您的部分結束時。嘗試:
^@[^(] ?\}\n(?=\@|$)
^@匹配行首的@[^(] ?匹配任何期望(一次或多次的字符,直到(?使它變得懶惰)下一個元素的下一次出現...\}\n您的段總是以大括號結尾,后跟新行(?=\@|$)關鍵部分:前瞻,確保以下元素是@(新段)或檔案末尾,而不捕獲它。這樣您就不會在結束之前剪切一個片段,您仍然可以捕捉下一個片段的開頭。
嘗試演示
uj5u.com熱心網友回復:
用
(?m)^@. (?:\n. )*
請參閱正則運算式證明。
解釋
--------------------------------------------------------------------------------
^ the beginning of the line due to (?m)
--------------------------------------------------------------------------------
@ '@'
--------------------------------------------------------------------------------
. any character except \n (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
\n '\n' (newline)
--------------------------------------------------------------------------------
. any character except \n (1 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
)* end of grouping
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/336641.html
下一篇:正則運算式獲取字串的一部分
