Python 正则表达式:结合句子的否定和肯定表达
大家好, 我正在用python编写一个网络爬虫。因为我在使用BeautifulSoup时遇到了一些问题,所以我正在深入研究正则表达式 我的问题集如下:假设我的文本如下所示:Python 正则表达式:结合句子的否定和肯定表达,python,regex,web-crawler,Python,Regex,Web Crawler,大家好, 我正在用python编写一个网络爬虫。因为我在使用BeautifulSoup时遇到了一些问题,所以我正在深入研究正则表达式 我的问题集如下:假设我的文本如下所示: <title>Document</title> <head>test and whatever</head> <body style="font-family:Times New Roman;font-size:10pt;">test and not whateve
<title>Document</title>
<head>test and whatever</head>
<body style="font-family:Times New Roman;font-size:10pt;">test and not whatever.</body>
<body style="font-family:Times New Roman;font-size:10pt;">test and not whatever.
This is a text after the dot. And this is a text after the dot but with test in it.
And a text with test and not.</body>
我试过了
[^\>|.]*(?=test)((?!not).)[^\<|.]*
[^\>|.]*(?=test)((?!not)。[^\(1)我建议你想办法用BeautifulSoup解决你的问题你不需要使用正则表达式做任何事情,你只需要找到包含test
的句子,然后使用简单的python删除那些包含not
的句子。嘿,Alex,beautiful soup很不幸再也没有机会了。首先获取所有包含“test”的句子,然后删除其中包含“not”的句子可能导致失败o更多的计算时间?我实际上希望这行正则表达式不是一个大交易。复杂的正则表达式速度很慢。如果你得到一个答案,你可以衡量性能,我猜[如果'not'not in sent'发送为sent in句子]
会快得多。你可能是对的。我的计划是只计算有多少句子包含test
(但不是不是,以排除否定句)。实际上,我甚至不需要知道句子的确切内容(1)我建议你找出如何解决BeautifulSoup的问题。(2)你不需要使用正则表达式做任何事情,你只需要找到包含test
的句子,然后使用简单的python删除那些包含not
的句子。嘿,Alex,beautiful soup很不幸再也没有机会了。首先获取所有包含“test”的句子,然后删除其中包含“not”的句子可能导致失败o更多的计算时间?我实际上希望这行正则表达式不是一个大交易。复杂的正则表达式速度很慢。如果你得到一个答案,你可以衡量性能,我猜[如果'not'not in sent'发送为sent in句子]
会快得多。你可能是对的。我的计划是只计算有多少句子包含test
(但不是不是,以排除否定句)。实际上我甚至不需要知道句子的确切内容
test and whatever
And this is a text after the dot but with test in it
[^\>|.]*(?=test)((?!not).)[^\<|.]*