Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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 将正则表达式作为参数传递的BeautifulSoup_Python_Regex_Python 2.7_Beautifulsoup - Fatal编程技术网

Python 将正则表达式作为参数传递的BeautifulSoup

Python 将正则表达式作为参数传递的BeautifulSoup,python,regex,python-2.7,beautifulsoup,Python,Regex,Python 2.7,Beautifulsoup,我有这个html: title="Keeper: Michal Buchalik" class="pos_text">Buchalik</a></span> <span class="pos_text pos3_l_5"> 它与任何内容都不匹配,并且正则表达式肯定有问题,因为当我键入某个数字来代替\d{1,2}时,它工作正常 由于是p

我有这个html:

title="Keeper: Michal Buchalik" class="pos_text">Buchalik</a></span>                
                                            <span class="pos_text pos3_l_5">

它与任何内容都不匹配,并且正则表达式肯定有问题,因为当我键入某个数字来代替
\d{1,2}
时,它工作正常

由于是python,您需要使用r表示“原始文本”或转义“\”字符:

re.compile(r"pos_text pos3_l_\d{1,2}")

OR

re.compile("pos_text pos3_l_\\d{1,2}")
看看是否有帮助


干杯。

谢谢您的回复,但在上述情况下不需要逃跑。请注意,在原始HTML页面中,没有反斜杠字符。是的,但您的正则表达式使用\d(数字)结构。因此,需要转义的不是文本,而是正则表达式本身。试一试?似乎问题不在正则表达式上-因为re.compile(“pos_text pos3\u l\d{1,2}”).split()似乎按预期运行。我在正则表达式中发现了一个明显的错误,这部分代码缺少
类=
,尽管它仍然没有给我预期的输出。
re.compile(r"pos_text pos3_l_\d{1,2}")

OR

re.compile("pos_text pos3_l_\\d{1,2}")