Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在正则表达式模式中使用反斜杠?_Python_Regex_Backslash - Fatal编程技术网

Python 如何在正则表达式模式中使用反斜杠?

Python 如何在正则表达式模式中使用反斜杠?,python,regex,backslash,Python,Regex,Backslash,简单的正则表达式任务:在字符串中查找ID(和语言) import re txt = '<OB02 ID="1099367" LANG="FR">' pattern = r'\\ID="(.*?)\\"' result = re.findall(pattern, txt) 重新导入 txt=“” pattern=r'\\ID=“(.*?\\”” 结果=re.findall(模式,txt) 这将导致一个空列表。引出以下问题: 如何在python中正确封装\ 如何从txt中提取I

简单的正则表达式任务:在字符串中查找ID(和语言)

import re

txt = '<OB02 ID="1099367" LANG="FR">'
pattern = r'\\ID="(.*?)\\"'

result = re.findall(pattern, txt)
重新导入
txt=“”
pattern=r'\\ID=“(.*?\\””
结果=re.findall(模式,txt)
这将导致一个空列表。引出以下问题:

  • 如何在python中正确封装\
  • 如何从txt中提取ID和LANG
使用解析xml而不是正则表达式。 作为解决方法,您可以使用以下正则表达式:

重新导入
txt=“”
模式='ID=“([^”]*)”
结果=re.findall(模式,txt)

如前所述,这是一个坏主意,因为如果有人现在开始使用单引号或添加注释,这将中断。

related:这也是我的第一个选择。但是对于这个字符串不起作用(我没有合适的XML)。但是如果您找到XML.etree.ElementTree可以处理这个问题的方法,我们非常欢迎。