Saturday 24 September 2016

java - Regex match for a character without previous character




I have the following String:



"location-string:location-string:location-C?:\string"



which I would like to split into the following three Strings:



location-string location-string location-C?:\string



What should the regex expression be when using String.split(regex)?




Basically, I want to split on colon ':' characters except those that are preceded by a '?' character!



Thanks in advance,
PM.


Answer



You could use negative lookbehind. It matches the colon which was not preceeded by ?



(?



Java regex would be,



"(?


DEMO


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...