简体   繁体   English

如何在Python中使用正则表达式匹配<和>之类的符号?

[英]How do I match for symbols like < and > with regex in Python?

I am parsing a log file, and I am trying to match for expressions like "key"=>"value" to extract the value. 我正在解析一个日志文件,并且试图匹配诸如"key"=>"value"类的表达式以提取该值。 I can't figure out how to match for the greater than symbol. 我不知道如何匹配大于符号。

I am not having any luck with re.match(r">", ...) , r"\\>" , or r"\\\\r" . 我没有re.match(r">", ...)r"\\>"r"\\\\r"运气。 How do I do this? 我该怎么做呢?

re.match only matches from the start of the string. re.match仅从字符串开头开始匹配。 try re.search: 尝试重新搜索:

>>> re.match(">", "a>b")
>>> re.search(">", "a>b")
<_sre.SRE_Match object at 0x7f4dd577e3d8>

or match the other text first: 或首先匹配其他文本:

>>> re.match(".*>", "a>b")
<_sre.SRE_Match object at 0x7f4dd577e440>

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

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