![](/img/trans.png)
[英]How to extract text part from file using Python & Regular Expressions
[英]How to extract text from between html tag using Regular Expressions?
我需要從textarea標簽之間提取文本。
我該如何使用正則表達式呢?
<textarea rows="20" cols="70" name="file" id="file" style="width: 100%;"data-input-file="1">
abc_text
#include<abc>
xyz
</textarea>
你可以試試,
>>> print [x.strip() for x in re.findall('<textarea.*?>(.*)</textarea>', content, re.MULTILINE | re.DOTALL)]
['abc_text\n #include<abc>\n xyz']
根據XML規則,XML無效。 開頭和結尾標簽不匹配。
#include<abc>
<abc>
是開始標記,不是內容。
XML解析庫不會解析無效的Input。
修改輸入:
如果將#include<abc>
更改為#include<abc>
那么以下將起作用:
>>> import lxml.html as PARSER
>>> root = PARSER.fromstring(data)
>>> root.xpath("//textarea/text()")
['\n abc_text\n #include<abc>\n xyz\n']
>>>
通過RE:
>>> data
'<textarea rows="20" cols="70" name="file" id="file" style="width: 100%;"data-input-file="1">\n abc_text\n</textarea>'
>>> import re
>>> re.findall('<textarea[^>]*>[^<]*</textarea>', data)
['<textarea rows="20" cols="70" name="file" id="file" style="width: 100%;"data-input-file="1">\n abc_text\n</textarea>']
>>>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.