我做了一个应用程序,我可以在匹配器的帮助下通过正则表达式得到字段的值...我做了一个方法,我传递一个字段并获得响应。在今天的字符串中,我得到了一些奇怪的行为,我得到了AgentId = 25001220052805950,在匹配后我得到fake
所以我必须检查名称中包含“AgentId”的字段是否存在并验证值。
需要的字段:
SecondaryAgentId=fake;
PrimaryAgentId=fake;
回复:
IsPrimaryAgentId=true; AgentId=25001220052805950; MerchantID=19; Cashier=michael; IsManualPayment=1; UserID=GraceRose; Password=rose1234; AmountUserEntered=2; AmountApproved=0; AmountDifference=0; Amount=0; CustomerNameAttempts=0; ProductID=Agriculture; InvoiceID=inv7443; SiteUrl=http://www.thcelink.com/index.php/shoping/checkout/step/step-1; ReturnURL=http://220.2.3:2027/Customer/Thanks.aspx; ResponseType=1; PrimaryAgentId=fake; PrimaryCurrencyCode=fake; SecondaryAgentId=fake; SecondaryCurrencyCode=fake; MerchantName=GraceRose; EmailId=rr@myglobal.com; Query1Attempts=0; MerchantTransactionID=543; MerchantTransactionSequenceID=246; txtAmtIsVisible=false; isQuery1Executed=false; isQuery2Executed=false; Voucher=fake; Passcode=fake; Error=fake; QueryType=fake; Payer=fake; CurrencyName=fake; CurrencySymbol=fake; CustomerName=fake; EmailBody=fake; ErrorText=fake; CustomerEmailID=fake; NavigatePageValue=0; IsCustomerInsertSucess=false; IdType=fake; IdNumber=fake; AggregateAttempts=0; Voucher2=fake; PassCode2=fake; Voucher3=fake; PassCode3=fake; TransCode=0; TransactionDate=2012-06-11T12:04:52.921875+05:30; NumberInWords=fake; MerchantCompany=fake; InvoiceNumber=fake; OverPaidAmount=0; InsufficientAmount=0; OverPaymentForEmail=fake; RedirectPage=false;
更新::
private String GetString1(String strManualproResponce2, String paternField) {
// TODO Auto-generated method stub
String s = null;
if(paternField.equalsIgnoreCase("AgentId"))
{
Pattern pinPattern2 = Pattern.compile("^"+paternField + "=(.*?);");
ArrayList<String> pins2 = new ArrayList<String>();
Matcher m2 = pinPattern2.matcher(strManualproResponce2);
while (m2.find()) {
pins2.add(m2.group(1));
s = m2.group(1);
}
}else
{
Pattern pinPattern2 = Pattern.compile(paternField + "=(.*?);");
ArrayList<String> pins2 = new ArrayList<String>();
Matcher m2 = pinPattern2.matcher(strManualproResponce2);
while (m2.find()) {
pins2.add(m2.group(1));
s = m2.group(1);
}
}
return s;
}
答案 0 :(得分:1)
你的问题有点神秘,据我所知,当你想匹配/提取AgentId
字段的值时代码不起作用。问题似乎与您的正则表达式有关:"^"+paternField + "=(.*?);"
假设文本AgentId
将位于字符串的开头,而不是因为在字符串的开头有IsPrimaryAgentId
。
此外,您的当前正则表达式将在IsPrimaryAgentId
和AgentId
都返回true,因为它们都包含子字符串:AgentId
。要解决此问题,您可以使用此正则表达式:\\s+AgentId=(.*?);
,这需要AgentId
文本前的空格。
另一种选择是(如果您的AgentId
始终为数字),请使用此选项:AgentId=(\\d+);
。