Tuesday, 15 July 2014

java - Regex: If there is a single colon, then add another colon -



java - Regex: If there is a single colon, then add another colon -

i using replaceall() alter single colons double colons way of "escaping" them. if entered 2 colons or more, not escape colons.

i tried /(:)([^::])/g regex expression. removes charter after colon e.g:

10:11:12 becomes 10::1::2

search (?<!:):(?!:) , replace ::

where (?<!:) negative lookbehind assumes there're no colon before colon , (?!:) loolahead assumes there're no colon after colon.

documentation on lookarround

code:

string s = "10:11:12 78:::78"; system.out.println(s.replaceall("(?<!:):(?!:)", "::"));

output:

10::11::12 78:::78

java regex

No comments:

Post a Comment