我想打破.
上的字符串(句号)。
实施例。 String str="We are going there.How are you."
然后输出
We are going there.
How are you.
它应该拆分为“。” 但如果我的字符串是
Dr.Harry is going.
那么它不应该像
Dr.
Harry is going.
它应该是Dr.Harry is going.
。
就像我有一些话,如果他们有字符串,那么它不应该破坏
StringBuffer regex = new StringBuffer("Dr[\\.]|Gr[\\.]|[Aa][\\.][Mm][\\.]|"+ "[Pp][\\.][Mm][\\.]|Emp[\\.]|Rs[\\.]|Ms[\\.]|No[\\.]|Nos[\\.]|"+ "Dt[\\.]|Sh[\\.]|(Mr|mr)[\\.]|(Mrs|mrs)[\\.]|Admn[\\.]|Ad[\\.]|Smt[\\.]|"+ "GOVT[\\.]|Govt[\\.]|Deptt[\\.]|Tel[\\.]|Secy[\\.]|Estt[\\.]|"+ "Asstt[\\.]|Hqrs[\\.]|DY[\\.]|Supdt[\\.]|w[\\.]e[\\.]f[\\.]|"+ "I[\\.]|N[\\.]|[0-9]+[\\.][0-9]+[\\.][0-9]|K[\\.]|NSI[\\.]|"+ "Prof[\\.]|Dte[\\.]|no[\\.]|nos[\\.]|Agri[\\.]|R[\\.]|"+ "K[\\.]|Y[\\.]|C[\\.]|N[\\.]|Dept[\\.]|S[\\.]|Spl[\\.]|N[\\.]|"+ "Sr[\\.]|Addl[\\.]|i[\\.]e[\\.]|Sl[\\.]|CS[\\.]|M[\\.]|IPS[\\.]|"+ "Jt[\\.]|viz[\\.]|hrs[\\.]|S/Sh[\\.]|Jr[\\.]|E[\\.]|S[\\.]|"+ "Pers[\\.]|Deptts[\\.]|OM[\\.]|DT[\\.]|Proj[\\.]|Instrum[\\.]|"+ "Div[\\.]|Dev[\\.]|Env[\\.]|e[\\.]g[\\.]|etc[\\.]|Misc[\\.]|"+ "vig[\\.]|Dr[\\.]|Nos[\\.]|Ltd[\\.]|Maj[\\.]|"+ "Gen[\\.]|MAJ[\\.]|GEN[\\.]|Su[\\.]|/Ess[\\.]|Com[\\.]|St[\\.]|");
这些是一些字符串,如果它们来的话不应该分割。就像Dr.Harry一样。 有可能是正则表达吗? 或任何其他方法? 谢谢
答案 0 :(得分:1)
使用它:
搜索:(?<!(Mr|Dr|Gr|Aa))\.
替换:\n
您可以在|
之后使用Aa
添加任意数量的字词。
演示:http://regex101.com/r/fP6hN9
我尝试了下面的代码,它对我来说很好用:
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
String str1 = "We are going there.How are you.Mr.Gordon is also coming with us.Are you sure you want to take him", str2;
String substr = "\n", regex = "(?<!(Mr|Dr|Gr|Aa))\\.";
// prints string1
System.out.println("String = " + str1);
/* replaces each substring of this string that matches the given
regular expression with the given replacement */
str2 = str1.replaceAll(regex, substr);
System.out.println("After Replacing = " + str2);
}
}
输出:
We are going there
How are you
Mr.Gordon is also coming with us
Are you sure you want to take him
答案 1 :(得分:1)