Value between brackets with a sperator character [modified]
-
I'm looking for a regex which can match the a value between brackets and deal with a seperator character within. This is the format I'm after: [?#?] where ? can be one or more characters and # is '+', '-' or '=' It should match the following. [1+1] [A=T] [AB=#1#2#3] So far I've got this: \[.*[\+-=]?\] But it doesn't work well, it includes text between the 'tags'. [A=B]ShouldNotInclude[1=3] (but it does ) I'm obviously doing something wrong,the whole regex probably. Anyone got an idea? Found the problem, not the solution. Problem is that it jumps to the last ']' character it can detect.
modified on Thursday, May 26, 2011 9:15 AM
-
I'm looking for a regex which can match the a value between brackets and deal with a seperator character within. This is the format I'm after: [?#?] where ? can be one or more characters and # is '+', '-' or '=' It should match the following. [1+1] [A=T] [AB=#1#2#3] So far I've got this: \[.*[\+-=]?\] But it doesn't work well, it includes text between the 'tags'. [A=B]ShouldNotInclude[1=3] (but it does ) I'm obviously doing something wrong,the whole regex probably. Anyone got an idea? Found the problem, not the solution. Problem is that it jumps to the last ']' character it can detect.
modified on Thursday, May 26, 2011 9:15 AM
-
I'm looking for a regex which can match the a value between brackets and deal with a seperator character within. This is the format I'm after: [?#?] where ? can be one or more characters and # is '+', '-' or '=' It should match the following. [1+1] [A=T] [AB=#1#2#3] So far I've got this: \[.*[\+-=]?\] But it doesn't work well, it includes text between the 'tags'. [A=B]ShouldNotInclude[1=3] (but it does ) I'm obviously doing something wrong,the whole regex probably. Anyone got an idea? Found the problem, not the solution. Problem is that it jumps to the last ']' character it can detect.
modified on Thursday, May 26, 2011 9:15 AM
What you saw is called 'greedy' matching. The better regex engines support non-greedy matching operators, which behave a bit more intuitively, even if they make the regex unreadable! Cheers, Peter
Software rusts. Simon Stephenson, ca 1994.