String srcString = "{ITEM_LT=AA, ITEM_DESC=BB, REQ_ITEM_CD=, ITEM_EN_NM= 000.0}";
Map paramMap = new Map();
Pattern pattern = Pattern.compile("(([^\\{,\\s]+)=([^,\\}]*))");
Matcher matcher = pattern.matcher(srcString);
out.println(matcher.matches()); // 전체문장에 대해한 match 여부이다.
out.println("<br/>");
out.println(matcher.groupCount()); // 괄호로 묶은 그룹 갯수이다. 여기서는 3개이다.
out.println("<br/>");
while (matcher.find())
{
// match된 전체 문장
out.println(String.format("%d %d %s", matcher.start(), matcher.end(), matcher.group()));
out.println("<br/>");
for (int i = 0; i <= matcher.groupCount(); i++)
{
// match된 개별 문장
out.println(String.format("%d : [%s]", i, matcher.group(i)));
out.println("<br/>");
}
}
false
3
1 11 ITEM_LT=AA
0 : [ITEM_LT=AA]
1 : [ITEM_LT=AA]
2 : [ITEM_LT]
3 : [AA]
13 25 ITEM_DESC=BB
0 : [ITEM_DESC=BB]
1 : [ITEM_DESC=BB]
2 : [ITEM_DESC]
3 : [BB]
27 39 REQ_ITEM_CD=
0 : [REQ_ITEM_CD=]
1 : [REQ_ITEM_CD=]
2 : [REQ_ITEM_CD]
3 : []
41 59 ITEM_EN_NM= 000.0
0 : [ITEM_EN_NM= 000.0]
1 : [ITEM_EN_NM= 000.0]
2 : [ITEM_EN_NM]
3 : [ 000.0]
* 메소드
/*
* Map형 literal 문자 -> Map으로 변환
* 문자열 형태 "{ITEM_LT=AA, ITEM_DESC=BB, REQ_ITEM_CD=java,test, ITEM_EN_NM= 000.0}" -> Map
*/
public static Map literalToMap(String literal)
{
Map rtnMap = new Map();
if (literal != null)
{
Pattern pattern = Pattern.compile("([^\\{,\\s]+)=([^=]*(?=, .+=|\\}))");
Matcher matcher = pattern.matcher(literal);
while (matcher.find())
{
rtnMap.put(matcher.group(1), matcher.group(2));
}
}
return rtnMap;
}
/*
* List형 literal 문자 -> List으로 변환
* 문자열 형태 "[{ITEM_LT=AA, ITEM_DESC=BB, REQ_ITEM_CD=, ITEM_EN_NM= 000.0}, {ITEM_LT=AA, ITEM_DESC=BB, REQ_ITEM_CD=, ITEM_EN_NM= 000.0}]" -> List<Map>
*/
public static List<Map> literalToList(String literal)
{
List<Map> rtnList = new List<Map>();
if (literal != null)
{
Pattern pattern = Pattern.compile("^\\[\\s*\\{.+\\}\\s*\\]$");
Matcher matcher = pattern.matcher(literal);
if (matcher.matches())
{
pattern = Pattern.compile("(\\{[^\\}]*\\})");
matcher = pattern.matcher(literal);
while (matcher.find())
{
rtnList.add(literalToMap(matcher.group(1)));
}
}
}
return rtnList;
}