我有以下响应我想解析它以获取使用正则表达式用等号(=
)分隔的名称和值:
anyType{
IsFound=true;
SubscriberId=7;
FormMode=Update;
MobileNumber=0785347032;
SimNumber=78987530047;
FirstName=Heba;
SecondName=Ali;
ThirdName=Ali;
LastName=Ali;
DateOfBirth=2001-01-01T00:00:00;
Sex=1;
Address=Zarqa;
ReigonId=null;
DistrictId=null;
Email=test@gmail.com;
IdentityNo=12312;
IdentityTypeId=3;
ContactNo1=0789876576;
ContactNo2=0789876456;
}
anyType{
IsFound=true;
SubscriberId=7;
FormMode=Update;
MobileNumber=0785347032;
SimNumber=78987530047;
FirstName=Heba;
SecondName=Ali;
ThirdName=Ali;
LastName=Ali;
DateOfBirth=2001-01-01T00:00:00;
Sex=1;
Address=Zarqa;
ReigonId=null;
DistrictId=null;
Email=test@gmail.com;
IdentityNo=12312;
IdentityTypeId=3;
ContactNo1=0789876576;
ContactNo2=0789876456;
}
我正在执行以下操作:(我想用正则表达式替换它)
public static ArrayList<ParsedPair> parse(String string)
{
ArrayList<ParsedPair> parsedPairs = new ArrayList<ParsedPair>();
String[] parts = string.split("anyType\\{");
if (parts != null && parts.length != 0)
parts[parts.length-1].replace("}", "");
for (int i=0; i<parts.length; i++)
{
String[] pairs = parts[i].split(";");
for (int j=0; j<pairs.length; j++)
{
ParsedPair parsedPair = new ParsedPair();
parsedPair.setName(pairs[j].substring(0, pairs[j].indexOf("=")).trim());
parsedPair.setValue(pairs[j].substring(pairs[j].indexOf("=")+1).trim());
parsedPairs.add(parsedPair);
}
}
return parsedPairs;
}
另一个问题:任何想法这种形式的数据格式化叫什么?是否有解析器?
答案 0 :(得分:0)
我不确定你的真正目标是什么,但也许这个简单的解析器会帮助你一点
public static List<Map<String, String>> parse(String input) {
List<Map<String, String>> result = new ArrayList<>();
boolean insideBraces = false;
boolean appendToKeys = true;
StringBuilder keyBiulder = new StringBuilder();
StringBuilder valueBiulder = new StringBuilder();
Map<String, String> map = null;
for (char ch : input.toCharArray()){
if (ch == '{'){
insideBraces = true;
map = new LinkedHashMap<>();//I assume that order of keys does matter
}else if (ch == '}'){
insideBraces = false;
result.add(map);
}else if (!Character.isWhitespace(ch) && insideBraces){//based on your example we ignore spaces
if (ch == '=') appendToKeys = false;//from now we need to append to valueBuilder
else if (ch == ';'){//no more data for this pair
map.put(keyBiulder.toString(), valueBiulder.toString());
//lets reset settings for next pair
appendToKeys = true;//we will start appending to keyBuilder again
keyBiulder.delete(0, keyBiulder.length());
valueBiulder.delete(0, valueBiulder.length());
}else{
if (appendToKeys) keyBiulder.append(ch);
else valueBiulder.append(ch);
}
}
}
return result;
}
用法示例
public static void main(String... args) {
String str="anyType{IsFound=true; SubscriberId=7; FormMode=Update; MobileNumber=0785347032 ; SimNumber=78987530047 ; FirstName=Heba; SecondName=Ali ; ThirdName=Ali\n" +
"; LastName=Ali ; DateOfBirth=2001-01-01T00:00:00; Sex=1; Address=Zarqa ; ReigonId=null; DistrictId=null; Email=test@gmail.com ; IdentityNo=12312; IdentityTypeId=3; ContactNo1=0789876576 ; ContactNo2=0789876456 ; } anyType{IsFound=true; SubscriberId=7; FormMode=Update; MobileNumber=0785347032 ; SimNumber=78987530047 ; FirstName=Heba; SecondName=Ali ; ThirdName=Ali\n" +
"; LastName=Ali ; DateOfBirth=2001-01-01T00:00:00; Sex=1; Address=Zarqa ; ReigonId=null; DistrictId=null; Email=test@gmail.com ; IdentityNo=12312; IdentityTypeId=3; ContactNo1=0789876576 ; ContactNo2=0789876456 ; }";
for (Map<String, String> map : parse(str)){
for (Map.Entry<String, String> entry : map.entrySet())
System.out.println(entry);
System.out.println("--------");
}
}
Ouptut:
IsFound=true
SubscriberId=7
FormMode=Update
MobileNumber=0785347032
SimNumber=78987530047
FirstName=Heba
SecondName=Ali
ThirdName=Ali
LastName=Ali
DateOfBirth=2001-01-01T00:00:00
Sex=1
Address=Zarqa
ReigonId=null
DistrictId=null
Email=test@gmail.com
IdentityNo=12312
IdentityTypeId=3
ContactNo1=0789876576
ContactNo2=0789876456
--------
IsFound=true
SubscriberId=7
FormMode=Update
MobileNumber=0785347032
SimNumber=78987530047
FirstName=Heba
SecondName=Ali
ThirdName=Ali
LastName=Ali
DateOfBirth=2001-01-01T00:00:00
Sex=1
Address=Zarqa
ReigonId=null
DistrictId=null
Email=test@gmail.com
IdentityNo=12312
IdentityTypeId=3
ContactNo1=0789876576
ContactNo2=0789876456
--------
答案 1 :(得分:0)
这应该有效,
String str="anyType{IsFound=true; SubscriberId=7; FormMode=Update; MobileNumber=0785347032 ; SimNumber=78987530047 ; FirstName=Heba; SecondName=Ali ; ThirdName=Ali\n" +
"; LastName=Ali ; DateOfBirth=2001-01-01T00:00:00; Sex=1; Address=Zarqa ; ReigonId=null; DistrictId=null; Email=test@gmail.com ; IdentityNo=12312; IdentityTypeId=3; ContactNo1=0789876576 ; ContactNo2=0789876456 ; } anyType{IsFound=true; SubscriberId=7; FormMode=Update; MobileNumber=0785347032 ; SimNumber=78987530047 ; FirstName=Heba; SecondName=Ali ; ThirdName=Ali\n" +
"; LastName=Ali ; DateOfBirth=2001-01-01T00:00:00; Sex=1; Address=Zarqa ; ReigonId=null; DistrictId=null; Email=test@gmail.com ; IdentityNo=12312; IdentityTypeId=3; ContactNo1=0789876576 ; ContactNo2=0789876456 ; }";
Pattern p=Pattern.compile("(\\w+)=(\\w+)\\s*;");
Matcher m=p.matcher(str1);
while(m.find()){
System.out.println(m.group(1)+"\t\t"+m.group(2));
}