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

标签 java regex

这是我正在写作的类(class):

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 代码没有找到任何匹配项呢?

最佳答案

您需要启用 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

关于java - 为什么这个正则表达式找不到匹配项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20690196/

相关文章:

java - 由 : java. lang.NoClassDefFoundError 引起:com/amazonaws/auth/BasicAWSCredentials

javascript - 删除标记名内字符串的一部分(通过js)

java - 使用 ArrayList 的优先级队列的性能

javascript - 如何 trim 多个字符?

ruby - 转义 TextMate 的 $DIALOG 命令的 Ruby 字符串

javascript - 复杂正则表达式

regex - 匹配 gsub/r 中的最后一个和第一个括号并保持剩余内容不变

java - 在 Apache Derby 中创建用户定义的聚合函数

java - 为什么第一次交换尝试有效,但第二次无效?

Java 异常无需代码进行