基于模式java拆分给定的文本

时间:2013-08-08 05:19:55

标签: java regex split

以下是我的文字

This is my first java Program. I am new to java program. but my program is successfully running without any. issues. Thanks to all

它应该像

一样分开
This is my first java Program.
I am new to java program. but my program is successfully running without any. issues.
Thanks to all.

正则表达式应采用以下模式

1.dot
2.followed by space
3.followed by capital letter word(not lowecase)

我试过这个

  1. \\.\\w[A-Z]
  2. \\.\\s\s[A-Z]
  3. \\.(?!\\w)
  4. 但未能得到它。

2 个答案:

答案 0 :(得分:2)

请尝试以下代码:

String text = "This is my first java Program. I am new to java program. but my program is successfully running without any. issues. Thanks to all";
Pattern pattern = Pattern.compile("(?<=\\.)\\s+(?=[A-Z])");
String[] lines = pattern.split(text);
for (String line : lines) {
    System.out.println(line);
}

输出:

This is my first java Program.
I am new to java program. but my program is successfully running without any. issues.
Thanks to all

答案 1 :(得分:0)

使用String.split()方法拆分字符串(返回一个数组)。

阅读here以了解java的正则表达式。