Finding an Exact Substring in a String in Python

Finding an exact substring in a string in Python

You should return False if it isn't (otherwise it'll return None):

def is_subsequence(sub, string):
if sub in string:
return True #if sub is a subsequence of str otherwise False
return False

or even better:

def is_subsequence(sub, string):
return sub in string

How to find exact sub-string in pandas?

This should work:

 newdf[newdf['id'].str.contains('49483$', regex=True)] 

#Out[216]:
# id collection_id dc_language_iso
#0 dli_ndli/49483 NaN English

python exact substring match

What you need is re.search with \b.

import re
if re.search(r"\b"+dish+r"\b",sentence):

Python - Return starting index of exact match to sub-string

If you are fine using a regex then this answer should address your issue.

Applying that to your question. We get

import re
a = re.search(r'\b(you)\b', 'Your family and you are invited to the party')
print a.start()

Which gives 16

Does this work for all possible positions of "you"? (start, middle and end)? Let's check

str1 = "you hi"
str2 = "hi you"
str3 = "hi you hi"
re.search(r'\b(you)\b', str1).start()
# output is 0
re.search(r'\b(you)\b', str2).start()
# output is 3
re.search(r'\b(you)\b', str3).start()
# output is 3

UPDATE 1: Case insensitive matching

In case you want a case insensitive match use re.IGNORECASE like this

re.search(r'\b(you)\b', str3, re.IGNORECASE).start()

UPDATE 2: Passing a variable instead of hardcoded string in the regex

str = "Your family and you are invited to the party"
word_to_search = "you"
re_string = r"\b({})\b".format(word_to_search)
re.search(re_string, str).start()
#output is 16

How to extract substring with Python Regex Exact Match

Instead of using anchors to match the whole line, you can use negative lookarounds to assert a whitespace boundary to the left, and not a / to the right to match 3/10 only.

(?<!\S)\d+\/\d{2}(?!/)

Regex demo

import re
txt = "my mood is low 3/10. 05/01/2021 Tuesday"
print(re.findall('(?<!\S)\d+\/\d{2}(?!/)', txt))

Output

['3/10']

How to find all the exact matches in a given string using python Regex

^[A-Z]{3} will match only 3 characters from the start of the string.

Try re.findall(r'\b[A-Z]{3}\b', a) which will match word boundaries appropriately.



Related Topics



Leave a reply



Submit