1

Iam trying to get all the indexes of specific group of special character ,but my regex is returning the index of single character of that group,

here is my code:-

a=re.finditer(r"[/ \\][\\ /]","/ \\ / \\ / \\ / /")
print([m.start(0) for m in a])

its returning [0,2,4,6,8,10,12]

but i want [0,2,4,6,8,10] , i only want index of "/ \" and "\ /"

4
  • @theausome yes output should be that, i've edited my post Commented Mar 30, 2018 at 15:04
  • Then try re.finditer(r"(/) (\\)|(\\) (/)", "/ \\ / \\ / \\") and grab the appropriate group start() values. Commented Mar 30, 2018 at 15:05
  • @WiktorStribiżew thanks man that worked Commented Mar 30, 2018 at 15:07
  • why my question is down voted :( Commented Mar 30, 2018 at 15:09

2 Answers 2

1

You should remove the character class brackets and wrap each symbol with a capturing group. Then, you may use some logic to grab the indices of the groups that matched.

import re
res = []
for m in re.finditer(r"(/) (\\)|(\\) (/)", "/ \\ / \\ / \\"):
    if m.group(1):
        res.extend([m.start(1), m.start(2)])
    else:
        res.extend([m.start(3), m.start(4)])

print(res) # => [0, 2, 4, 6, 8, 10]

See the online Python demo.

Here, if Group 1 matched, that means we need to grab the start indices of Group 1 and 2, else, the start indices of Group 3 and 4.

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

2 Comments

nah it ain't returning the correct output, can u check it with string "/ \\ / \\ / \\ / /" ,its printing [0,4,8]
@AbhishekKumar It prints [0, 2, 4, 6, 8, 10]
0

I found an better answer for that, its short one liner

print([m.start(0) for m in re.finditer(r"(?=(/ \\|\\ /))", "/ \\ / \\ / \\ / /")])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.