Firstly, I know the similar questions that were asked here:
How to split a line but also keep the delimiters?
However, I had a problem with the implementation of line splitting using Pattern.split (), where the pattern is based on a list of separators, but sometimes they may overlap. Here is an example:
The goal is to split the string based on a set of known codewords that are surrounded by slashes, where I need to store both the delimiter (codeword) and the value after it (which can be an empty string).
In this example, the code words are:
/ABC/
/DEF/
/GHI/
Based on the stream mentioned above, the template is constructed as follows, using look-forward and look-behind to mark the string with AND codeword values:
((?<=/ABC/)|(?=/ABC/))|((?<=/DEF/)|(?=/DEF/))|((?<=/GHI/)|(?=/GHI/))
Working line:
"123/ABC//DEF/456/GHI/789"
split, :
"123","/ABC/","/DEF/","456","/GHI/","789"
( "ABC" "DEF" ):
"123/ABC/DEF/456/GHI/789"
, "DEF/456" "/ABC/", "DEF/" , !
:
"123","/ABC/","DEF/456","/GHI/","789"
:
"123","/ABC","/","DEF/","456","/GHI/","789"
, "ABC" "DEF" .
, look-ahead OR look-behind, , , . !