I need to split data by dot. And I have escaped dot(.), that I should ignore. Also I should ignore escaped backslash too (\). For example,
data1\\.d\\\\\.ata2\\\\.da\.ta3.data4
This string should be splitted to for substrings like as
data1\\
d\\\\\.ata2\\\\
da\.ta3
data4
I cannot to create regex for that. Do you know, it is possible? I tried to use following:
(?<!\\((\\\\){2,}))\\. - not working
I can create following regex if escaped slash defined only one time:
"((?<!\\\\)\\.)|((?=([^\\\\]*((\\\\\\\\)+[^\\\\]*)))\\.)";
For example data1\\.d\.ata2.da\.ta3.data4 splitted correctly:
data1\\
d\.ata2
da\.ta3
data4
But I cannot detect backslash definition even number times. Can you help me, please? Thank you!
You may extract these strings using
(?s)(?:[^\\.]|\\.)+
See the regex demo. Details:
(?s) - enable the Pattern.DOTALL flag so that . could match across lines(?:[^\\.]|\\.)+ - one or more occurrences of any char other than \ and ., or a \ followed with any char.See a Java demo:
String line = "data1\\\\.d\\.ata2.da\\.ta3.data4";
Pattern p = Pattern.compile("(?s)(?:[^\\\\.]|\\\\.)+");
Matcher m = p.matcher(line);
List<String> res = new ArrayList<>();
while(m.find()) {
res.add(m.group());
}
System.out.println(res);
// => [data1\\, d\.ata2, da\.ta3, data4]
You may use this regex to get your matches:
(?=[^.])[^.\\]*(?:\\.[^.\\]*)*(?=\.|$)
RegEx Demo:
(?=[^.]): Make sure there is non-dot character ahead[^.\\]*: Match 0+ of any character that is not a . not a \(?:\\.[^.\\]*)*: A non-capture group that matches an backslash followed by an escaped character and that should be followed by 0 or more of any character that is not a . not a \. Match 0 or more of this group(?=\.|$): Make sure we have a dot or end of line ahead