问题
word = 'laugh'
string = 'This is laughing laugh'
index = string.find ( word )
index is 8, should be 17. I looked around hard, but could not find an answer.
回答1:
You should use regex (with word boundary) as str.find
returns the first occurrence. Then use the start
attribute of the match
object to get the starting index.
import re
string = 'This is laughing laugh'
a = re.search(r'\b(laugh)\b', string)
print(a.start())
>> 17
You can find more info on how it works here.
回答2:
try this:
word = 'laugh'
string = 'This is laughing laugh'.split(" ")
index = string.index(word)
This makes a list containing all the words and then searches for the relevant word. Then I guess you could add all of the lengths of the elements in the list less than index and find your index that way
position = 0
for i,word in enumerate(string):
position += (1 + len(word))
if i>=index:
break
print position
Hope this helps.
回答3:
Here is one approach without regular expressions:
word = 'laugh'
string = 'This is laughing laugh'
# we want to find this >>> -----
# index 0123456789012345678901
words = string.split(' ')
word_index = words.index(word)
index = sum(len(x) + 1 for i, x in enumerate(words)
if i < word_index)
=> 17
This splits the string into words, finds the index of the matching word and then sums up the lengths and the blank char as a separater of all words before it.
Update Another approach is the following one-liner:
index = string.center(len(string) + 2, ' ').find(word.center(len(word) + 2, ' '))
Here both the string
and the word
are right and left padded with blanks as to capture the full word in any position of the string.
You should of course use regular expressions for performance and convenience. The equivalent using the re
module is as follows:
r = re.compile(r'\b%s\b' % word, re.I)
m = r.search(string)
index = m.start()
Here \b
means word boundary, see the re documentation. Regex can be quite daunting. A great way to test and find regular expressions is using regex101.com
回答4:
Strings in code are not separated by spaces. If you want to find the space, you must include the space in the word you are searching for. You may find it would actually be more efficient for you to split the string into words then iterate, e.g:
str = "This is a laughing laugh"
strList = str.split(" ")
for sWord in strList:
if sWord == "laugh":
DoStuff()
As you iterate you can add the length of the current word to an index and when you find the word, break from the loop. Don't forget to account for the spaces!
来源:https://stackoverflow.com/questions/38956274/how-to-find-index-of-an-exact-word-in-a-string-in-python