[英]Splitting a string by using two substrings in Python
我正在使用re
搜索字符串,這幾乎適用於所有情況,除非有換行符(\\ n)
例如,如果string定義為:
testStr = " Test to see\n\nThis one print\n "
然后像這樣搜索re.search('Test(.*)print', testStr)
不會返回任何內容。
這里有什么問題? 我該如何解決?
re模塊有re.DOTALL表示“。”。 也應該匹配換行符。 一般 ”。” 匹配除換行符之外的任何內容。
re.search('Test(.*)print', testStr, re.DOTALL)
或者:
re.search('Test((?:.|\n)*)print', testStr)
# (?:…) is a non-matching group to apply *
例:
>>> testStr = " Test to see\n\nThis one print\n "
>>> m = re.search('Test(.*)print', testStr, re.DOTALL)
>>> print m
<_sre.SRE_Match object at 0x1706300>
>>> m.group(1)
' to see\n\nThis one '
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.