如何在java中使用正则表达式使用项目符号(所有类型)拆分字符串
下面是我的文本如何在java中使用正则表达式使用项目符号(所有类型)拆分字符串,java,regex,split,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)•
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
谢谢这将起作用:
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
你也可以试试这个
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);
}
发出
Welcome to java programming
Oops concepts
Encapsulation
Abstraction
Inheritance
Polymorphism
}
输出
欢迎使用java编程面向对象的概念
封装
抽象
继承
多态性
java上的示例程序
项目源代码示例文本文件
sample.txt
sam.txt
以下基于正向前瞻的正则表达式适用于您:
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]
你想在每个项目后面插入句号吗,还是这是一个输入错误?它适用于我上面的句子,但不适用于其他语句,例如:John提供CC cources 1。他还提供了一些其他资源。它包括Company Inc.
[\\d\\w]
与刚才的\\w
完全相同,因为单词chars包括数字。您的值类似于1)Oops概念,我的问题是您是否有11)或110)而不是1)。你有什么建议?我没有。你也许可以问另一个问题。它适用于我上面的句子,但不适用于其他语句。例如:John提供CC cources 1。他还提供了一些其他资源。它包括Company Inc。它应该按照类似于这种模式的州划分。
Split => [Welcome to java programming, Oops concepts, Encapsulation, Abstraction, Inheritance, Polymorphism]