-1

我做了一个应用程序,我可以在匹配器的帮助下通过正则表达式获取字段的值......我制作了一个方法,在其中传递一个字段并获得响应。在今天的字符串中,我的响应中有一些奇怪的行为,我得到了 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;
    }
4

1 回答 1

1

你的问题有点神秘,据我了解,当你想匹配/提取AgentId字段的值时,代码不起作用。问题似乎与您的正则表达式有关:"^"+paternField + "=(.*?);"假设文本AgentId将位于字符串的开头,而不是因为您拥有的字符串的开头IsPrimaryAgentId

此外,您当前的正则表达式将返回 true IsPrimaryAgentIdAgentId因为它们都包含子字符串:AgentId。要解决此问题,您可以使用此正则表达式: ,这将需要文本\\s+AgentId=(.*?);前的空格。AgentId

另一种选择是(如果你AgentId总是数字)使用这个:AgentId=(\\d+);.

于 2012-06-11T06:58:19.323 回答