+1 vote
in Programming Languages by (43.2k points)

I am trying to find the index of characters '[' and ']' in a string using the "re" library.


vals = "## URLs * URL: [https ://test.com/diseases/cancer](https ://test.com/diseases/cancer)"

strt = [m.start() for m in re.finditer('[', vals)]

 The finditer() function returns the following error: "re.error: unterminated character set at position 0". How to fix it?

1 Answer

+1 vote
by (54.5k points)
selected by
Best answer

You need to use the escape() function for '[' or ']' because these characters have special meaning in the "re" library.

You can try the following code, and it should fix the issue.

str1 = re.escape("[")

strt = [m.start() for m in re.finditer(str1, vals)]