I have a String needs to be split by semi-colon. The easiest way to go is [^;]+. By using this, every semi-colon in the string will be used as a delimiter.
However, in our string, there are some “&” that we do not want to include that ; as a delimiter.
For example, a String
abcd;efg;hij&kl;mn
The expected result would be abcd, efg, hij&kl, mn
Can some one help me to solve this problem?
If you want to match everything outside of “real
;s”:would work. Or
(?:&\w+;|[^;])+if more than just&entities are to be expected.If your regex engine supports split operations, perhaps this regex (matching semicolons only if not preceded by
&) is also a good ideaTo also allow other entities like above,
(?<!&\w+);can be used if your regex implementation supports indefinite repetition inside lookbehind assertions. Most don’t, though, .NET being an exception.In Python: