2015-12-08 48 views
1

我想显示文本中的所有行,但目前我只显示第二行。该错误是在我rowRegex :( 我将是任何帮助,非常感谢选择文本中的所有图案

我的代码:。

package test.jpa; 

import java.util.regex.Matcher; 
import java.util.regex.Pattern; 


public class FireTest { 

    public static void main(String[] arStrings) throws Exception { 

     String str = "*PureNG*#" 
        + "\"*Part*\":https://pure1.pdf** \"Part1\":https://pure2.pdf** \"Part2\":https://pure3.pdf** \"Part3\":https://pure4.pdf#" 
        + "\"*Fakt*\":https://pure5.pdf** \"Fakt1\":https://pure5.pdf** \"Fakt2\":https://pure6.pdf#" 
        + " \"*WZ* _(wydanie)_\":https://pure7.pdf#" 
        + " \"*ZA*\":https://pure8.pdf** \"PA\":https://pure9.pdf#" 
        + " \"*Close*\":https://pure11.pdf** \"Close1\":https://pure12.pdf#" 
        + " \"*Stany*\":https://pure13.pdf</text><version>7</version><author id=\"1\" name=\"UserName LastName Admin\"/><comments></comments> <created_on>2015-11-26T15:08:26Z</created_on><updated_on>2015-11-30T15:44:00Z</updated_on></wiki_page>"; 

     String rowRegex = "(#.*?#|\\Z)"; 
     Pattern patternRow = Pattern.compile(rowRegex); 
     Matcher matcher = patternRow.matcher(str); 

     while(matcher.find()) { 
      System.out.println("Finded: " + matcher.group()); 
     } 
    } 
} 

那个是我的结果的那一刻:

Finded: #"*Part*":https://pure1.pdf** "Part1":https://pure2.pdf** "Part2":https://pure3.pdf** "Part3":https://pure4.pdf# 
Finded: # "*WZ* _(wydanie)_":https://pure7.pdf# 
Finded: # "*Close*":https://pure11.pdf** "Close1":https://pure12.pdf# 
Finded: 

但我会想获得后续输出:

Finded: #"*Part*":https://pure1.pdf** "Part1":https://pure2.pdf** "Part2":https://pure3.pdf** "Part3":https://pure4.pdf 
Finded: #"*Fakt*":https://pure5.pdf** "Fakt1\":https://pure5.pdf** "Fakt2":https://pure6.pdf 
Finded: # "*WZ* _(wydanie)_":https://pure7.pdf 
Finded: # "*ZA*":https://pure8.pdf** "PA":https://pure9.pdf 
Finded: # "*Close*":https://pure11.pdf** "Close1":https://pure12.pdf 
Finded: # *Stany*\":https://pure13.pdf</text><version>7</version><author id=\"1\" name=\"UserName LastName Admin\"/><comments></comments><created_on>2015-11-26T15:08:26Z< created_on><updated_on>2015-11-30T15:44:00Z</updated_on></wiki_page>" 
+0

请问您可以以可读格式发布输入吗?谢谢! – zolo

+0

@Derek,请考虑接受最适合您的答案。 –

回答

0

试试这个

"(#?.*?#|\\Z)" 

,所以你需要#?代替#

1

你的正则表达式必须看起来像

String rowRegex = "#[^#]++"; 

IDEONE demo

#匹配文字#[^#]++将匹配1或除了#(所有者)之外的更多符号。

如果您需要丢弃第一个#,请使用捕获组#([^#]++)并使用matcher.group(1)访问第一个组。

+0

@Derek,有什么消息吗?你有没有时间检查我的解决方案? –