Python用正则表达式匹配一个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19300020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python match a string with regex
提问by Zack
I need a python regular expression to check if a word is present in a string. The string is separated by commas, potentially.
我需要一个 python 正则表达式来检查字符串中是否存在一个单词。字符串可能用逗号分隔。
So for example,
例如,
line = 'This,is,a,sample,string'
I want to search based on "sample", this would return true. I am crappy with reg ex, so when I looked at the python docs, I saw something like
我想根据“样本”进行搜索,这将返回true。我对 reg ex 很蹩脚,所以当我查看 python 文档时,我看到了类似的东西
import re
re.match(r'sample', line)
But I don't know why there was an 'r' before the text to be matched. Can someone help me with the regular expression?
但我不知道为什么要匹配的文本前有一个 'r'。有人可以帮助我使用正则表达式吗?
采纳答案by jabaldonedo
Are you sure you need a regex? It seems that you only need to know if a word is present in a string, so you can do:
你确定你需要一个正则表达式吗?似乎您只需要知道字符串中是否存在某个单词,因此您可以执行以下操作:
>>> line = 'This,is,a,sample,string'
>>> "sample" in line
True
回答by Zack
The r
makes the string a raw string, which doesn't process escape characters (however, since there are none in the string, it is actually not needed here).
这r
使字符串成为原始字符串,它不处理转义字符(但是,由于字符串中没有转义字符,因此这里实际上不需要它)。
Also, re.match
matches from the beginning of the string. In other words, it looks for an exactmatch between the string and the pattern. To match stuff that could be anywhere in the string, use re.search
. See a demonstration below:
此外,re.match
从字符串的开头匹配。换句话说,它寻找字符串和模式之间的精确匹配。要匹配字符串中任何位置的内容,请使用re.search
. 请参阅下面的演示:
>>> import re
>>> line = 'This,is,a,sample,string'
>>> re.match("sample", line)
>>> re.search("sample", line)
<_sre.SRE_Match object at 0x021D32C0>
>>>
回答by mlnyc
r stands for a raw string, so things like \ will be automatically escaped by Python.
r 代表一个原始字符串,所以像 \ 这样的东西会被 Python 自动转义。
Normally, if you wanted your pattern to include something like a backslash you'd need to escape it with another backslash. raw strings eliminate this problem.
通常,如果您希望模式包含反斜杠之类的内容,则需要使用另一个反斜杠对其进行转义。原始字符串消除了这个问题。
In your case, it does not matter much but it's a good habit to get into early otherwise something like \b will bite you in the behind if you are not careful (will be interpreted as backspace character instead of word boundary)
在您的情况下,这并不重要,但尽早进入是一个好习惯,否则如果您不小心,诸如 \b 之类的东西会在后面咬您(将被解释为退格字符而不是单词边界)
As per re.match vs re.search here's an example that will clarify it for you:
根据 re.match 与 re.search,这里有一个示例可以为您澄清:
>>> import re
>>> testString = 'hello world'
>>> re.match('hello', testString)
<_sre.SRE_Match object at 0x015920C8>
>>> re.search('hello', testString)
<_sre.SRE_Match object at 0x02405560>
>>> re.match('world', testString)
>>> re.search('world', testString)
<_sre.SRE_Match object at 0x015920C8>
So search will find a match anywhere, match will only start at the beginning
所以搜索会在任何地方找到匹配,匹配只会从头开始
回答by Inbar Rose
You do not need regular expressions to check if a substring exists in a string.
您不需要正则表达式来检查字符串中是否存在子字符串。
line = 'This,is,a,sample,string'
result = bool('sample' in line) # returns True
If you want to know if a string contains a patternthen you should use re.search
如果你想知道一个字符串是否包含一个模式,那么你应该使用re.search
line = 'This,is,a,sample,string'
result = re.search(r'sample', line) # finds 'sample'
This is best used with pattern matching, for example:
这最好与模式匹配一起使用,例如:
line = 'my name is bob'
result = re.search(r'my name is (\S+)', line) # finds 'bob'
回答by Peter Party Bus
As everyone else has mentioned it is better to use the "in" operator, it can also act on lists:
正如其他人所说,最好使用“in”运算符,它也可以作用于列表:
line = "This,is,a,sample,string"
lst = ['This', 'sample']
for i in lst:
i in line
>> True
>> True
回答by rahul mehra
One Liner implementation:
一个班轮实施:
a=[1,3]
b=[1,2,3,4]
all(i in b for i in a)