如何使用re排除单词? [英] How can I exclude a word by using re?
问题描述
然后,标点符号^表示标点符号^。可以排除单个字符,但我想要现在排除整个单词的
。例如我有一个字符串你怎么样?
你。你好,我想在世界之前提取所有部分你好,
我不能使用。* [^ hello]"因为^仅排除单个字符h或者
e或l或l。或o。有人会告诉我该怎么做吗?谢谢。
In re, the punctuation "^" can exclude a single character, but I want
to exclude a whole word now. for example I have a string " how are
you. hello", I want to extract all the part before the world "hello",
I can''t use ".*[^hello]" because "^" only exclude single char "h" or
"e" or "l" or "o". Will somebody tell me how to do it? Thanks.
推荐答案
re.findall(''(。*)hello |(。*)'',''你好吗?你好'')
re.findall(''(。*)你好|(。*)'',''你好吗.ello'')
看看这些的输出。
re.findall(''(.*)hello|(.*)'', '' how are you. hello'')
re.findall(''(.*)hello|(.*)'', '' how are you. ello'')
take a look at the outputs of these.
可能ildg写道:
在重新标点符号中,^ ;可以排除单个字符,但我希望现在排除整个单词。例如我有一个字符串你是怎么回事? 你好,我想在世界之前提取所有部分你好,我不能使用。* [^ hello]"因为^仅排除单个字符h或者是e或l或l。或o。有人会告诉我该怎么做吗?谢谢。
In re, the punctuation "^" can exclude a single character, but I want
to exclude a whole word now. for example I have a string " how are
you. hello", I want to extract all the part before the world "hello",
I can''t use ".*[^hello]" because "^" only exclude single char "h" or
"e" or "l" or "o". Will somebody tell me how to do it? Thanks.
导入重新
def演示(正则表达式,文本):
pattern = re.compile(regex)
match = pattern.search(text)
print" ",text
如果匹配:
print"匹配''%s''' %match.group(0)
print"捕获''%s''' %match.group(1)
else:
print"不符合
#选项1:匹配所有内容,但只捕获hello之前的部分。
(。*?)
#匹配尽可能少的字符,这样这个模式就会结束
#hello hello中的第一个问候语。
pattern = r"(。*?)hello"
print" Option 1 :",模式
演示(模式,你好,你好。你好)
#选项2:不要匹配你好,但请确保它在那里。
#这些调用中的第一个将匹配,但第二个不匹配。
#(?= ...)构造使用了一个名为前瞻预测的功能。
打印\ nOption 2:",模式
演示(模式,你好,你好。你好 ;)
演示(模式,你好吗。)
import re
def demonstrate(regex, text):
pattern = re.compile(regex)
match = pattern.search(text)
print " ", text
if match:
print " Matched ''%s''" % match.group(0)
print " Captured ''%s''" % match.group(1)
else:
print " Did not match"
# Option 1: Match it all, but capture only the part before "hello." The
(.*?)
# matches as few characters as possible, so that this pattern would end
before
# the first hello in "hello hello".
pattern = r"(.*?)hello"
print "Option 1:", pattern
demonstrate( pattern, " how are you. hello" )
# Option 2: Don''t even match the "hello," but make sure it''s there.
# The first of these calls will match, but the second will not. The
# (?=...) construct is using a feature called "forward look-ahead."
pattern = r"(.*)(?=hello)"
print "\nOption 2:", pattern
demonstrate( pattern, " how are you. hello" )
demonstrate( pattern, " how are you. ", )
谢谢。
但是,如果有多个你好,我该怎么办?我只想要
来提取第一个你好之前的内容。例如,原始的
字符串是你好吗?你好,我很好,谢谢你好。那就是它'
你好',我想在第一次你好之前提取所有东西吗?
2005年8月14日08:02:16 - 0700,Christoph Rackwitz
< ch **************** @ gmail.com>写道:
Thank you.
But what should I do if there are more than one hello and I only want
to extract what''s before the first "hello". For example, the raw
string is " how are you? hello I''m fine, thank you hello. that''s it
hello", I want to extract all the stuff before the first hello?
On 14 Aug 2005 08:02:16 -0700, Christoph Rackwitz
<ch****************@gmail.com> wrote:
re.findall(''(。*)hello |(。*)'',''你好。你好'')
re.findall(' '(。*)你好|(。*)'',''你好吗.ello'')看看这些产品的输出。
-
http://mail.python.org/mailman/listinfo / python-list
re.findall(''(.*)hello|(.*)'', '' how are you. hello'')
re.findall(''(.*)hello|(.*)'', '' how are you. ello'')
take a look at the outputs of these.
--
http://mail.python.org/mailman/listinfo/python-list
这篇关于如何使用re排除单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!