Java – a regular expression used to split strings separated by | when not enclosed in double quotes
•
Java
I need a regular expression to calculate the number of columns in the pipeline delimited string in Java
For example:
"1234"|"Name"||"Some description with ||| in it"|"Last Column"
The above contents shall be counted as 5 columns, including an empty column after the "name" column
thank you
Solution
This is a method:
String input = "\"1234\"|\"Name\"||\"Some description with ||| in it\"|\"Last Column\""; // \_______/ \______/\/\_________________________________/ \_____________/ // 1 2 3 4 5 int cols = input.replaceAll("\"[^\"]*\"","") // remove "..." .replaceAll("[^|]","") // remove anything else than | .length() + 1; // Count the remaining |,add 1 System.out.println(cols); // 5
IMO is not very powerful For example, if you plan to handle escaped quotes, I recommend not using regular expressions
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码