如何使用java中的regex将项目符号(所有类型)拆分为字符串

时间:2013-08-08 07:02:51

标签: java regex split

以下是我的文字

 Welcome to java programming 1) Oops concepts a) Encapsulation A) Abstraction I) Inheritance  • Polymorphism
2)sample program on java 1.Project source code 
sample text files • sample.txt b)sam.txt

我想根据以下模式拆分文本并删除子弹

1)any bullet 1)I)a)A)•
2)followed by space
3)followed by uppercase word

所以我想生成以下结果

Welcome to java programming 
Oops concepts 
Encapsulation 
Abstraction
Inheritance  
Polymorphism
sample program on java
Project source code
Please suggest me how to do this
sample text files
sample.txt

sam.txt 感谢

4 个答案:

答案 0 :(得分:1)

这将有效:

public static void main(String[] args) throws Exception {
    final String s = "Welcome to java programming 1) Oops concepts a) Encapsulation A) Abstraction I) Inheritance  • Polymorphism";
    final String[] split = s.split("\\s*(\\w\\)|•)\\s*");
    for (final String bullet : split) {
        System.out.println(bullet);
    }
}

正则表达式是

\\s*(\\w\\)|•)\\s*
  • \\s* - 零个或多个空格
  • (\\w\\)|•) - 数字或字母后跟括号或项目符号
  • \\s* - 零个或多个空格

输出:

Welcome to java programming
Oops concepts
Encapsulation
Abstraction
Inheritance
Polymorphism

答案 1 :(得分:1)

基于正向前瞻的正则表达式应该适合您:

String s = "Welcome to java programming 1) Oops concepts a) Encapsulation A) Abstraction I) Inheritance  • Polymorphism";
String[] arr = s.split("\\s*([a-zA-Z\\d][).]|•)\\s*(?=[A-Z])");
System.out.println("Split => " + Arrays.toString(tok));

<强>输出:

Split => [Welcome to java programming, Oops concepts, Encapsulation, Abstraction, Inheritance, Polymorphism]

答案 2 :(得分:0)

你也可以尝试这个

String str="Welcome to java programming 1) Oops concepts a) Encapsulation A) Abstraction I) Inheritance  • Polymorphism" ;
    String newStr=str.replaceAll("(?i)\\s*([\\d\\w]\\)|•)\\s*"," ISseperatorIS ");
    String[] arr=newStr.split("ISseperatorIS ");
    for(String i:arr){
        System.out.println(i);
    }

Out put

Welcome to java programming 
Oops concepts 
Encapsulation 
Abstraction 
Inheritance 
Polymorphism

答案 3 :(得分:0)

public class Bullet {
public static void main(String [] args){
    String str = "Welcome to java programming 1) Oops concepts a) Encapsulation A) Abstraction I) Inheritance  • Polymorphism2)sample program on java 1.Project source code sample text files • sample.txt b)sam.txt";
    String [] bullet = str.split("[0-9]\\)|[A-Z]\\)|[a-z]\\)|•|[0-9]\\.");
    for(String i:bullet){
        System.out.println(i);
    }
}

}

输出&LT; \ p为H.

欢迎来到java编程
 哎呀概念
 封装
 抽象
 继承
 多态性
java上的示例程序 项目源代码示例文本文件
 sample.txt
sam.txt