簡體   English   中英

正則表達式解析xml字符串

[英]regex to parse xml string

我有一個pom.xml文件,我將其轉換為字符串並嘗試解析其依賴項。 例如

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
  </dependency>    
</dependencies>

我正在嘗試使用模式獲取dependency標簽之間的內容

({<}dependency{>}.*?{</}dependency{>})

我的目標是使所有文字都一樣。

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>  

正確的做法是什么?

嘗試這個

Pattern regex = Pattern.compile("<dependency>(.*?)</dependency>", Pattern.DOTALL);

Pattern.DOTALL是修飾符,用於匹配換行符的點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM