繁体   English   中英

匹配特殊字符之间的字符串

[英]Match string between special characters

我对正则表达式有些了解,但在很大程度上对它并不熟悉。 字符串将采用以下格式:

\n\n*text here, can be any spaces, etc. etc.*

我将得到的字符串将有两个换行符,后跟一个星号,然后是文本,然后以另一个星号结束。

我想从返回的文本中排除开头的\\n\\n 到目前为止,这是我想出的模式,并且似乎可行:

pattern = "(?<=\\n\\n)\*(.*)(\*)"

match = re.search(pattern, string)
if match:
    text = match.group()
    print (text)
else:
    print ("Nothing")

我想知道是否有更好的方法来匹配此模式,或者我处理它的方式是否可以。

谢谢。

您可以使用以下方法避免捕获组并获得整个匹配结果:

pattern = r'(?<=\n\n\*)[^*]*(?=\*)'

例:

import re
print re.findall(r'(?<=\n\n\*)[^*]*(?=\*)','\n\n*text here, can be any spaces, etc. etc.*')

如果要在结果中包含星号,则可以改用:

pattern = r'(?<=\n\n)\*[^*]*\*'

在这样的情况下,如果分隔符始终是静态的并且位于字符串的头部/尾部,则正则表达式会显得过大。

>>> s = "\n\n*text here, can be any spaces, etc. etc.*"
>>> def CheckString(s):
...     if s.startswith("\n\n*") and s.endswith("*"):
...         return s[3:-1]
...     else:
...         return "(nothing)"
>>> CheckString(s)
'text here, can be any spaces, etc. etc.'
>>> CheckString("no delimiters")
'(nothing)'

(根据需要调整切片索引-我尚不清楚是否要保留前导/后缀'*'字符。如果要保留它们,请将切片更改为

return s[2:]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM