我想显示文本中的所有行,但目前我只显示第二行。错误出现在我的 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>"
最佳答案
你的正则表达式必须看起来像
String rowRegex = "#[^#]++";
请参阅IDEONE demo
#
匹配文字 #
,[^#]++
将匹配 #< 以外的 1 个或多个符号
(所有格)。
如果您需要丢弃第一个#
,请使用捕获组#([^#]++)
并使用匹配器访问第一个组。组(1)
。
关于java - 选择文本中的所有模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34167866/