gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:39:02 24 4
gpt4 key购买 nike

这是我正在写作的类(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com