Java 用正则表达式提取以点和空间结尾的子序列
hy 我想用正则表达式提取这个句子的子句子:Java 用正则表达式提取以点和空间结尾的子序列,java,regex,pattern-matching,Java,Regex,Pattern Matching,hy 我想用正则表达式提取这个句子的子句子: 了解od fg网络布局。kdsjhuu ddkm networ.12kfdf。learndfefe布局。学习SDFFSFS。sddsd学习fefe。 我无法为模式编写正确的正则表达式。请编译 这是我的表达:([^(\\.\\s)]*)([^.]*\\) 事实上,我需要一种写“阅读除\\\.\\s以外的所有内容”的方法 分句: 了解od fg网络布局。 kdsjhuu ddkm网络.12kfdf. learndfefe布局。 学习SDFFSFS。 s
了解od fg网络布局。kdsjhuu ddkm networ.12kfdf。learndfefe布局。学习SDFFSFS。sddsd学习fefe。
我无法为模式编写正确的正则表达式。请编译
这是我的表达:([^(\\.\\s)]*)([^.]*\\)
事实上,我需要一种写“阅读除\\\.\\s以外的所有内容”的方法
分句:
了解od fg网络布局。
kdsjhuu ddkm网络.12kfdf.
learndfefe布局。
学习SDFFSFS。
sddsd学习fefe。
只需使用正则表达式拆分字符串即可。“
您可以将此模式与find
方法结合使用:
Pattern p = Pattern.compile("[^\\s.][^.]*(?:\\.(?!\\s|\\z)[^.]*)*\\.?");
Matcher m = p.matcher(yourText);
while(m.find()) {
System.out.println(m.group(0));
}
图案详情:
[^\\s.] # all that is not a whitespace (to trim) or a dot
[^.]* # all that is not a dot (zero or more times)
(?: # open a non-capturing group
\\. (?!\\s|\\z) # dot not followed by a whitespace or the end of the string
[^.]* #
)* # close and repeat the group as needed
\\.? # an optional dot (allow to match a sentence at the end
# of the string even if there is no dot)
为什么不直接使用String.split?例如>sentance.split(“.”),正则表达式以何种方式不起作用?问题在于这一部分:“networ.12kfdf”。我不需要拆分。我想使用Matcher,然后查找。我想在Matcher中使用此模式,然后在句子中查找。Matcher m=pattern.Matcher(句子);while(m.find())这是我的答案。谢谢。请原谅我,我是正则表达式的乞丐。如果我想在这个句子中找到包含“学习”或“学习…网络”的句子,我应该怎么做?
[^\\s.] # all that is not a whitespace (to trim) or a dot
[^.]* # all that is not a dot (zero or more times)
(?: # open a non-capturing group
\\. (?!\\s|\\z) # dot not followed by a whitespace or the end of the string
[^.]* #
)* # close and repeat the group as needed
\\.? # an optional dot (allow to match a sentence at the end
# of the string even if there is no dot)