I need two different Regex, to split the strings, For the following scenarios. Using Java platform.
1. Needs to Split by only first "/" and if the slash comes within "{}" braces then its wont be consider And also I need to truncate starting and ending braces "{}" only if it comes like example c).
a)
input: "Response/CartResponse/{StatusData/ResponseCode,RespMessage,{ArrivalMethods/AvailableArrivalMethod/AvailableArrivalMethodName,AvailableFFMCenter}},{ServiceHeaders/clientSessionKey},{Shoppingcart/OrderId,CatalogId}";
output:
"Response"
"CartResponse/{StatusData/ResponseCode,RespMessage,{ArrivalMethods/AvailableArrivalMethod/AvailableArrivalMethodName,AvailableFFMCenter}},{ServiceHeaders/clientSessionKey},{Shoppingcart/OrderId,CatalogId}";
-------------
b)
input: "CartResponse/{StatusData/ResponseCode,RespMessage,{ArrivalMethods/AvailableArrivalMethod/AvailableArrivalMethodName,AvailableFFMCenter}},{ServiceHeaders/clientSessionKey},{Shoppingcart/OrderId,CatalogId}";
output:
"CartResponse"
"{StatusDat开发者_StackOverflow中文版a/ResponseCode,RespMessage,{ArrivalMethods/AvailableArrivalMethod/AvailableArrivalMethodName,AvailableFFMCenter}},{ServiceHeaders/clientSessionKey},{Shoppingcart/OrderId,CatalogId}";
-------------
c)
input: "ArrivalMethods/{AvailableArrivalMethod/AvailableArrivalMethodName,AvailableFFMCenter}";
output:
"ArrivalMethods"
"AvailableArrivalMethod/AvailableArrivalMethodName,AvailableFFMCenter"
2. Split by comma and if the comma comes within "{}" braces then it wont be consider And also I need to remove "{}" as in the following examples.
input: "{StatusData/ResponseCode,RespMessage,{ArrivalMethods/AvailableArrivalMethod /AvailableArrivalMethodName,AvailableFFMCenter}},{ServiceHeaders/clientSessionKey},{Shoppingcart/OrderId,CatalogId}";
output:
"StatusData/ResponseCode,RespMessage,{ArrivalMethods/AvailableArrivalMethod/AvailableArrivalMethodName,AvailableFFMCenter}"
"ServiceHeaders/clientSessionKey"
"Shoppingcart/OrderId,CatalogId"
input: "ResponseCode,RespMessage,{ArrivalMethods/AvailableArrivalMethod/AvailableArrivalMethodName,AvailableFFMCenter}";
output:
"ResponseCode"
"RespMessage"
"ArrivalMethods/AvailableArrivalMethod/AvailableArrivalMethodName,AvailableFFMCenter";
Truncating braces is different in both cases.
The difference between case a,b and c is that a and b have embedded {} inside the {}, and c doesn't. Bad news, that cannot be achieved by a single regular expression. Even more, guaranteeing the balance of {} would be nightmarish.
Anyway, what you are describing here is basically a language so it is better to build a parser for it (either by hand or by using a tool like ANTLR or JavaCC).
This problem as you have described it can be solved using only string comparisons and manipulations, no regular expressions needed. Try writing a method which breaks each case into separate conditions and handles it accordingly, e.g.:
public String[] parseInput(String s) {
if (s.startsWith("Response/") || s.startsWith("CartResponse/")) {
// Examples (1.a), (1.b)
return str.split("/", 2);
} else if (str.startsWith("ArrivalMethods")) {
// Example (1.c)
String[] ss = str.split("/", 2);
ss[1] = ss[1].substring(1, ss[1].length() - 1);
return ss;
} else if (s.startsWith("{")) {
// Example (2.a)
// ...
}
return null;
}
精彩评论