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)
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
arr[hex] ? If yes, I updated my answer for thatThe 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.