2013-12-19 37 views
0

這裏是我的課我在寫作中我:爲什麼不是這個正則表達式找到一個匹配?

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: <[email protected]> 

<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:[email protected]%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: <[email protected]: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代碼找不到任何匹配?

回答

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

+0

是的,我的固定問題。我不知道我以前從未遇到過這個問題......爲什麼'find()'工作,但'matches()'不? –

+1

'String.matches()'是一種方便的方法,不使用MULTILINE模式。如果他們將標誌作爲參數包含在另一個方法中,那就太好了。但他們沒有,所以你必須做很長的路:) –

相關問題