Python match a string with regex

2020-02-10 11:33发布

I need a python regular expression to check if a word is present in a string. The string is separated by commas, potentially.

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

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?

标签: python regex
6条回答
贼婆χ
2楼-- · 2020-02-10 11:36

r stands for a raw string, so things like \ will be automatically escaped by 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.

short explanation

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)

As per re.match vs re.search here's an example that will clarify it for you:

>>> 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

查看更多
贼婆χ
3楼-- · 2020-02-10 11:37

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).

Also, re.match matches from the beginning of the string. In other words, it looks for an exact match between the string and the pattern. To match stuff that could be anywhere in the string, use re.search. See a demonstration below:

>>> import re
>>> line = 'This,is,a,sample,string'
>>> re.match("sample", line)
>>> re.search("sample", line)
<_sre.SRE_Match object at 0x021D32C0>
>>>
查看更多
在下西门庆
4楼-- · 2020-02-10 11:47

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
查看更多
你好瞎i
5楼-- · 2020-02-10 11:50

As everyone else has mentioned it is better to use the "in" operator, it can also act on lists:

line = "This,is,a,sample,string"
lst = ['This', 'sample']
for i in lst:
     i in line

>> True
>> True
查看更多
▲ chillily
6楼-- · 2020-02-10 11:57

One Liner implementation:

a=[1,3]
b=[1,2,3,4]
all(i in b for i in a)
查看更多
地球回转人心会变
7楼-- · 2020-02-10 12:01

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 pattern then you should use 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'
查看更多
登录 后发表回答