47

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?

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Zack
  • 13,454
  • 24
  • 75
  • 113
  • 4
    Why do you want a regex? `if 'sample' in line:` is much better for matching specific substrings. – Wooble Oct 10 '13 at 15:31
  • thanks, I think I am going to go with this. Just to clarify, this will return true for "sample" and "this,is,a,sample" ? – Zack Oct 10 '13 at 15:44
  • Yes, it will return True for anything that contains "sample". Regular expressions are overkill if you don't actually need to match a more complex pattern. – Wooble Oct 10 '13 at 15:46
  • nice, thanks for the tip. – Zack Oct 10 '13 at 15:47
  • What should the behavior be if the string is `samplestring,otherstring`? In the given answers, `sample` will match the string. You'll have to do something more complicated if you only want exact matches to the items in the comma-delimited list. – Neal Gokli Jun 25 '18 at 22:30

6 Answers6

77

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
jabaldonedo
  • 25,822
  • 8
  • 77
  • 77
  • 1
    the trouble is that it might not be a comma separated list. My input could be "Sample" or it could be "This,is,a,Sample" – Zack Oct 10 '13 at 15:37
  • 1
    You don't need to split it. If the searchable is a string it'll look for a substring. If its a list it'll look for an item in that list – mlnyc Oct 10 '13 at 15:38
  • 1
    @mlnyc is right, splitting creates a list of words and you compare those words with the given word, but `"sample" in line` looks for a substring that matches your word. – jabaldonedo Oct 10 '13 at 15:43
  • this is what actually helped me, so I marked as accepted. However, I'm not sure it is what best corresponds to the tags for the question, if others are searching for the same thing... oh well. – Zack Oct 10 '13 at 15:48
  • @Zack That's fine. If they have the same problem, they know that there're things simpler to use than regex :) – Jerry Oct 10 '13 at 15:53
  • Yes, I am sure I need regex, because if I did not need it, I would search for "python string contains" instead. – Dragas Mar 24 '22 at 11:24
30

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>
>>>
  • I might need to search from the beginning though, the word I'm looking for could be first – Zack Oct 10 '13 at 15:31
  • @Zack - My bad--I needed to explain better. `re.search` will match stuff that is first. Read my edited post. –  Oct 10 '13 at 15:32
9

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

mlnyc
  • 2,636
  • 2
  • 24
  • 29
5

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'
Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
2

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
Peter Party Bus
  • 2,326
  • 1
  • 14
  • 15
-3

One Liner implementation:

a=[1,3]
b=[1,2,3,4]
all(i in b for i in a)
Aurasphere
  • 3,841
  • 12
  • 44
  • 71
rahul mehra
  • 418
  • 2
  • 13