Regex 如何通过执行正则表达式向文件中添加字符串?
但是,文件some.txt中没有写入任何内容,您的正则表达式无法匹配。`/^'表示斜杠,后跟字符串的开头。尝试删除正则表达式中的两个斜杠。我得到一个日志文件,该文件记录了与以下更改“不匹配”的内容:Regex 如何通过执行正则表达式向文件中添加字符串?,regex,python-3.x,Regex,Python 3.x,但是,文件some.txt中没有写入任何内容,您的正则表达式无法匹配。`/^'表示斜杠,后跟字符串的开头。尝试删除正则表达式中的两个斜杠。我得到一个日志文件,该文件记录了与以下更改“不匹配”的内容: import re import logging x = input("enter number of bad car") if re.match('/^[A-ZА-Я]{1}[0-9]{3}[A-ZА-Я]{2}([0-9]{0,3})?$/', x): logging.debug("fou
import re
import logging
x = input("enter number of bad car")
if re.match('/^[A-ZА-Я]{1}[0-9]{3}[A-ZА-Я]{2}([0-9]{0,3})?$/', x):
logging.debug("found match" + x)
with open("some.txt" , 'w') as f:
f.write(x + '\n')
else:
logging.debug("does not match")
因此,我补充说:
logging.basicConfig
,它设置了一些基本配置
用于日志文件(名称、路径、格式…)needed
,其中包含所有需要的数据,然后使用logging.info()
56184615
:
import re
import logging
logging.basicConfig(filename = ( "loggingfile.log"), level = logging.DEBUG, format = '%(message)s')
x = input("enter number of bad car")
if re.match('/^[A-ZА-Я]{1}[0-9]{3}[A-ZА-Я]{2}([0-9]{0,3})?$/', x):
logging.debug("found match" + x)
with open("some.txt" , 'w') as f:
needed = str(x + '\n')
logging.info(needed)
else:
logging.info("does not match")
你的正则表达式匹配吗?您是否看到日志“找到匹配x”或“不匹配”?--注意你的else语句是过度缩进的。首先删除正则表达式分隔符。你应该能够一步一步地缩小它的范围。输出x。看看哪一部分失败了。
does not match