0

I want to get index from array.

For example:

s = "arr[2]"

I want to use regular expression for returning index[2]

Updated! I want also to return the name of array (arr)

0

4 Answers 4

3

This should do it the regex way! (Find all possible numbers between square brackets []

def get_idx(s):
    m = re.search(r"\[([-?a-zA-Z0-9_]+)\]", s)
    return m.group(1)

Then you can test it as

print(get_idx("arr[2]"))
#2
print(get_idx("arr[24]"))
#24
print(get_idx("arr[245]"))
#245
print(get_idx("arr[-2]"))
#-2
print(get_idx("arr2[-2]"))
#-2
print(get_idx("arr2[hex]"))
#hex

In order to get the name of the array, you can do

def get_name(s):
    m = re.search(r"(.*)\[.*", s)
    return m.group(1)

Then you can test it as

import re

def get_name(s):
    m = re.search(r"(.*)\[.*", s)
    return m.group(1)

print(get_name("arr[2]"))
#arr
print(get_name("arr[24]"))
#arr
print(get_name("arr[245]"))
#arr
print(get_name("arr[-2]"))
#arr
print(get_name("arr2[-2]"))
#arr2
Sign up to request clarification or add additional context in comments.

7 Comments

what if arr[-1] ?
Could you explain ? If I want to return the name of array then what is it regex ? @Devesh Kumar Singh
I have updated my answer for that case @Alex! Please check and consider accepting the answer if it works :)
Could you explain what to do ? For example, If index is hex. How to change it ? @ Devesh Kumar Singh
I didn't understand your question, you mean arr[hex] ? If yes, I updated my answer for that
|
0

There is way without regex

s[s.find("[")+1:s.find("]")]

If you want to use regex, you can use the following:

import re
re.search(r'\[(.*?)\]',s).group(1)

In both the methods output is as following:

2

Comments

0

This should work

re.search("\[(-?\d+)\]", s).group(1)

For more on Regex go through this link

1 Comment

I have edited it now. Thanks for correcting me. Since in Python indexing can even be negative, I have edited it accordingly.
0

The following is more than enough given your example:

import re
re.search(r"(.*)\[(.*)\]", s).groups()

Where s is your string and .groups() tells Python to return the groups found in the parentheses () as a tuple (<group1>, <group2>). To break it down:

  • . = any non-newline character
  • * = 0 or more times
  • () = delimit the group of characters to be extracted, otherwise the square brackets will be returned as well
  • \[ and \] = the square brackets need to be escaped with \ because they are regex special characters (read more about escapes here)

To put it together: Extract any group of 0 or more non-newline characters found before a square bracket [, and extract and group of 0 or more non-newline characters that can be found between two square brackets [ and ]. The other solutions also utilize the special character ? to make the match non-greedy, but it isn't necessary in your example as there is only one set of square brackets.

Besides being terser than the other solutions, it also deals with special cases better, and will extract everything found before or within the brackets, i.e. digits, + and -, and any indices given as variable names, such as x:

re.search(r"(.*)\[(.*)\]", "arr[23]").groups()
## ('arr', '23')

re.search(r"(.*)\[(.*)\]", "arr[-23]").groups()
## ('arr', '-23')

re.search(r"(.*)\[(.*)\]", "arr[+23]").groups()
## ('arr', '+23')

re.search(r"(.*)\[(.*)\]", "arr-2[23]").groups()
## ('arr-2', '23')

re.search(r"(.*)\[(.*)\]", "arr[x]").groups()
## ('arr', 'x')

re.search(r"(.*)\[(.*)\]", "arr[FF00EE88]").groups()
## ('arr', 'FF00EE88')

Also, don't forget that strings are being returned, so you'll need to coerce your array indices (the second value in the returned tuple) into integers with int if that's what you need.

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.