这是我的代码。我想忽略~~内的任何内容。即使它包含新行,白色空格。所以我可以忽略这些评论。
for letter in code :
tok += letter #adding each character to the token.
if not is_str and (tok == " " or tok == "\n"):
#ignoring whitespaces and new line if it's not a string.
tok = "" #reseting each the iterator token.
#Always always always remember. It's not lexer's job to generate errors
#It's the work of parser. One thing should only do one thing.
elif re.search(r'Enter', tok):
tokens.append("ENTER")
tok = ""
elif re.search(r'~(.*?|\n*?)~',tok):
#to ignore the comments written within ~this~
tok = ""
答案 0 :(得分:2)
您可以使用re.DOTALL
标志:
使
<div class="container"> <div class="dropdown"> <button onclick="myFunction()" class="dropbtn">Select City</button> <div id="myDropdown" class="dropdown-content"> <a href="#home">Mumbai</a> </div> </div> </div>
特殊字符与任何字符匹配,包括a 的换行强>;如果没有此标记,'.'
将匹配除换行符之外的任何内容。
'.'
<强>试验强>:
pattern = re.compile(r'~(.*?)~', re.DOTALL)
答案 1 :(得分:2)
如果~
字符串中不允许其他~
,您可以使用:
r'~[^~]*~'
这将匹配除~
之外的任何字符。