2

I need to find the starting index of a string when there is an exact match to a sub-string.

line = "Your family and You are invited to my party"

I want to find the starting index of Youi.e. 16

I have tried

line.find("You")

however that returns 0

Then I tried,

import re
print(re.findall('\\bYou\\b', line))

But it returns a list with the sub-string in it

["You"]
Bryce Ramgovind
  • 3,127
  • 10
  • 41
  • 72
  • 1
    That returns `0` because you have a string `"You"` at position `0`. The `"You"` of `"Your"`. I think what you are looking for in a exact match of a word, a word being a collection of alphabets separated by a space. – Haris Jan 20 '18 at 09:36
  • Good find @Rawing that answer should answer the OP's query – S Raghav Jan 20 '18 at 09:52

3 Answers3

3

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
S Raghav
  • 1,386
  • 1
  • 16
  • 26
  • How do I pass a variable into `re.search` with regular expressions i.e `re.search(r'\b('+key+')\b', str3).start()` where `key = 'you'` – Bryce Ramgovind Jan 20 '18 at 10:27
  • @BryceRamgovind refer to the second comment in the answer by DeepSpace (link in my answer). It says "Like you would with any Python string. You can concat or use .format, ie word = 'laugh' ; re.search(r'\b({})\b'.format(word), string)". I'll try it out and update my answer – S Raghav Jan 20 '18 at 10:31
  • @BryceRamgovind updated my answer after trying out the suggestion in the answer I linked. Feel free to comment if it doesn't work for you – S Raghav Jan 20 '18 at 10:38
1

Use re.search to get appropriate position of your pattern. For example:

import re
line = "Your family and You are invited to my party"
res = re.search('\\bYou\\b', line)

It gives a result that looks like

<_sre.SRE_Match object; span=(16, 19), match='You'>

Then

beg, end = res.span()

where variable beg stores required index.

eaniconer
  • 184
  • 2
  • 10
1

This Should work

import re
line = "Your family and You are invited to my party"
re.search('\\bYou\\b', line).start() 

to get the exact index

E_K
  • 2,159
  • 23
  • 39