首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么这只公鸡找不到匹配的?

为什么这只公鸡找不到匹配的?
EN

Stack Overflow用户
提问于 2013-12-19 19:34:55
回答 1查看 178关注 0票数 0

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

代码语言:javascript
复制
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

代码语言:javascript
复制
--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:的正则表达式进行搜索,它会找到两个结果。那么,为什么这些java代码找不到匹配的呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-19 19:36:24

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

代码语言:javascript
复制
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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20690196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档