为什么这个正则表达式找不到匹配?

时间:2013-12-19 19:34:55

标签: java regex

这是我的课程,我正在写作:

public class MtomParser {

    private static final String HEADER_REGEX = "^\\s*Content-ID:";

    public boolean isMtom(String payload) {
        return payload.contains("--uuid");
    }

    public String parseMtom(String mtomResponse) {
        while (mtomResponse.matches(HEADER_REGEX)) {
            System.out.println("header found");
        }
        return mtomResponse;
    }
}

我期待我的输入使得此代码导致无限循环,因为它应该找到匹配并且无法逃脱循环。但是,mtomResponse.matches(HEADER_REGEX)每次都返回false,我不知道为什么。这是mtomResponse

--uuid:b6bd1ef2-63e2-4d8d-8bac-eabbe7588373
        Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml";
        Content-Transfer-Encoding: binary
        Content-ID: <root.message@cxf.apache.org>

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Header/><soap:Body><RetrieveDocumentSetResponse xmlns="urn:ihe:iti:xds-b:2007" xmlns:ns10="http://docs.oasis-open.org/wsrf/bf-2" xmlns:ns11="http://docs.oasis-open.org/wsn/t-1" xmlns:ns12="urn:gov:hhs:fha:nhinc:common:subscriptionb2overridefordocuments" xmlns:ns13="http://nhinc.services.com/schema/auditmessage" xmlns:ns14="urn:oasis:names:tc:emergency:EDXL:DE:1.0" xmlns:ns15="http://www.hhs.gov/healthit/nhin/cdc" xmlns:ns16="urn:gov:hhs:fha:nhinc:common:subscriptionb2overrideforcdc" xmlns:ns2="urn:gov:hhs:fha:nhinc:common:nhinccommon" xmlns:ns3="urn:gov:hhs:fha:nhinc:common:nhinccommonentity" xmlns:ns4="urn:oasis:names:tc:ebxml-regrep:xsd:rim:3.0" xmlns:ns5="urn:oasis:names:tc:ebxml-regrep:xsd:rs:3.0" xmlns:ns6="urn:oasis:names:tc:ebxml-regrep:xsd:query:3.0" xmlns:ns7="urn:oasis:names:tc:ebxml-regrep:xsd:lcm:3.0" xmlns:ns8="http://docs.oasis-open.org/wsn/b-2" xmlns:ns9="http://www.w3.org/2005/08/addressing"><ns5:RegistryResponse status="urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:Success"/><DocumentResponse><HomeCommunityId>urn:oid:422.422</HomeCommunityId><RepositoryUniqueId>422.422</RepositoryUniqueId><DocumentUniqueId>422.422^C4n2hv7z_5Ofa37W</DocumentUniqueId><mimeType>text/xml</mimeType><Document><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:3511c0cc-5e20-46b7-8ae0-406c3b1ea95f-6@urn%3Aihe%3Aiti%3Axds-b%3A2007"/></Document></DocumentResponse></RetrieveDocumentSetResponse></soap:Body></soap:Envelope>
        --uuid:b6bd1ef2-63e2-4d8d-8bac-eabbe7588373
        Content-Type: text/xml
        Content-Transfer-Encoding: binary
        Content-ID: <3511c0cc-5e20-46b7-8ae0-406c3b1ea95f-6@urn:ihe:iti:xds-b:2007>

        <?xml version="1.0" encoding="UTF-8"?>
<ClinicalDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:hl7-org:v3" xsi:schemaLocation="urn:hl7-org:v3 http://hit-testing.nist.gov:11080/hitspValidation/schema/cdar2c32/infrastructure/cda/C32_CDA.xsd">
<realmCode code="US"/>
<typeId root="2.16.840.1.113883.1.3" extension="POCD_HD000040"/>

在我的IDE中,如果我使用^\s*Content-ID:的正则表达式进行搜索,则会找到2个结果。那么为什么这个java代码找不到任何匹配?

1 个答案:

答案 0 :(得分:4)

您需要启用MULTILINE模式,以允许^匹配每一行而不是整个字符串。

Pattern pattern = Pattern.compile(yourRegex, Pattern.MULTILINE);
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
    System.out.println(matcher.group());
}

请参阅:http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html