Reputation: 77
I'm having trouble with a regex. I basically want to remove everything but brackets, including 0,1,2,3,4,5,6,7,8,9,+,-,*,/
An example would be
(9*[3*{[(3+3)/5]*7}]) # should give the answer ([{[()]}])
My current regex is
[0123456789+-\\*/] # and it gives the answer ({()]}])
That's giving me the correct answer except [
is matching the regex, and I don't want it too. However ]
does not match so I am confused.
Upvotes: 3
Views: 67
Reputation: 5965
Easiest solution provided by @HamZa in the comments using the negative char ^
[^][(){}]
Another solution is to move the -
char to the start or the end of the regex, as in the current position is being treated a range character just like in A-Za-z
[0123456789+\\*/-]
Check it in http://www.regexpal.com/?fam=96675
Upvotes: 3
Reputation: 424993
Use the posix expression for "any non bracket":
[^\p{Ps}\p{Pe}]
and replace it with blank
\p{Ps}
or \p{Open_Punctuation}
: any kind of opening bracket.\p{Pe}
or \p{Close_Punctuation}
: any kind of closing bracket.Upvotes: 1