I need a regex to match a string as follows:
- Must begin with
[
- Must contain a
]
- Is allowed to have any characters (including whitespaces) between the
[
and the]
- Must contain at least one character between
[
and]
- Is allowed to have a
;
after the]
. Following the;
all characters are allowed (although sort of irrelevant since I don't care about it) - If and only if a
;
after a]
is present, whitespaces (read tabs, spaces - although I can guarantee no\r\n\f\v
will be present, which is why I used\s
below) are allowed between the]
and the;
. If;
is not present after the]
, then]
must be the end of the string.
I ended up with the following regex which passed all my initial tests: ^\[([^]]+)](?:\s+?;)?
.
Speed is key here, so I am looking to improve on the regex that I have in order to shave off a few cycles if possible.
I'm not really sure whether the usage of a lookahead would be useful here.
EDIT
eg:
[some;thing]
- Valid, with capture group some;thing
[something]
- Valid, with capture group something
[something]
- Invalid, does not begin with [
[something] ;ojasodj
- Valid, capture group something
[something]
- Invalid, space after ]
without a ;
present
[something];
- Valid, capture group something
[]
- Invalid, must contain at least one character between [
and ]