10

My Task

I am trying to find the position of words appearing in a string using regex

Code

import re # A random string mystr = "there not what is jake can do for you ask what you play do for spare jake".upper() match = re.search(r"[^a-zA-Z](jake)[^a-zA-Z]", mystr) print match.start(1) 

Output

18 

Expected output

I would expect my output to contain the positions of the string jake:

5, 17 

EDIT: To clarify, I'm trying to identify the position of words. I believe what I have done is found the index and am unsure how to make it work as I expect

6
  • 2
    @WiktorStribiżew, he wants word position, not character Commented May 22, 2017 at 9:23
  • @RomanPerekhrest: it is a rhetorical question. Wiktor wants to point out the wrong approach. The string is made out of characters, not of words. The OP is getting the index of the first character occurence. Commented May 22, 2017 at 9:26
  • Is this even possible with only regex? I don't think so. Commented May 22, 2017 at 9:31
  • Dupe of stackoverflow.com/a/12054409/3832970 Commented May 22, 2017 at 9:32
  • r'[\s]*(jake)[\s]*' would already be a better Regex, but I don't think this is possible with Regex only Commented May 22, 2017 at 9:33

2 Answers 2

10

To get the "ordinal" positions of search string jake in the input string use the following approach:

mystr = "there not what is jake can do for you ask what you play do for spare jake" search_str = 'jake' result = [i+1 for i,w in enumerate(mystr.split()) if w.lower() == search_str] print(result) 

The output:

[5, 17] 

  • enumerate(mystr.split()) - to get enumerated object (pairs of items with their positions/indices)

  • w.lower() == search_str - if a word is equal to search string

Sign up to request clarification or add additional context in comments.

3 Comments

I was writting pretty much the same, but you were faster. Easy and simple solution without using RegEx.
He asked using regex, right?
@cezar, yes, no need regex in this case and import re
4

Try this way:

mystr = "there not what is jake can do for you ask what you play do for spare jake" result = [index+1 for index,word in enumerate(mystr.split()) if word=='jake'] result 

Output:

[5, 17] 

3 Comments

you were 12 seconds later
And I was even more late than that, so discarded my answer. But you know that it takes much more than 12 seconds to write an answer. Speed is good, but accuracy is more important.
@RomanPerekhrest we are in same time space :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.