[英]Removing all html markup
我有一個字符串,其中包含完整的XML get請求。
在請求中,有很多HTML和一些我想刪除的自定義命令。
我知道的唯一方法是使用jSoup 。
例如這樣 。
現在,由於請求來自的網站還具有自定義命令,因此我無法完全刪除所有代碼。
例如,這是我要“ 清理 ”的字符串:
\u0027s normal text here\u003c/b\u003e http://a_random_link_here.com\r\n\r\nSome more text here
如您所見, 自定義命令前面都帶有反斜杠。
我將如何使用Java刪除這些命令?
如果我使用正則表達式,如何編程使其僅刪除命令,而不刪除命令后的任何內容? (因為如果我進行軟編碼:我事先不知道命令的大小,也不想對所有命令進行硬編碼)。
看到http://regex101.com/r/gJ2yN2
正則表達式(\\\\.\\d{3,}.*?\\s|(\\\\r|\\\\n)+)
用於刪除您指出的內容。
結果(用 單個空格):
normal text here http://a_random_link_here.com Some more text here
如果這不是您想要的結果,請用預期結果編輯您的問題。
編輯正則表達式解釋:
() - match everything inside the parentheses (later, the "match" gets replaced with "space")
\\ - an 'escaped' backslash (i.e. an actual backslash; the first one "protects" the second
so it is not interpreted as a special character
. - any character (I saw 'u', but there might be others
\d - a digit
{3,} - "at least three"
.*? - any characters, "lazy" (stop as soon as possible)
\s - until you hit a white space
| - or
() - one of these things
\\r - backslash - r (again, with escaped '\')
\\n - backslash - n
您向我們顯示的“自定義命令”似乎是標准字符轉義符。 \\ r是回車符,ASCII 13(十進制)。 \\ n是換行符,ASCII 10(十進制)。 \\ uxxxx通常是帶有該十六進制值的Unicode字符的轉義符-例如,\\ u0027是ASCII字符39,即撇號(')。 您不想丟棄這些; 它們是您要檢索的文本內容的一部分。
因此,最好的答案是確保您知道該數據集中要接受哪些轉義符,然后查找或編寫代碼以快速線性搜索查找\\的代碼,並在找到后使用下一個字符確定哪種類型的代碼。將其轉義(以及該轉義屬於多少個后續字符),將轉義序列替換為它代表的單個字符,然后繼續直到到達字符串/緩沖區/文件/所有內容的末尾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.