简体   繁体   English

在点 (.) 上拆分字符串,除非它出现在 Java 中使用正则表达式的分数中

[英]Splitting a string on dot (.) except if it appears in a fraction using regex in Java

As the title says, I want to split a string using java regex based on dot but only if the dot appears in between alphabets.正如标题所说,我想使用基于点的 java regex 拆分字符串,但前提是点出现在字母之间。

Lets say the string is:假设字符串是:

System.out.println(5.55);

I need output as我需要输出为

System
out
println(5.55);

Simply use lookahead and lookbehind like this只需像这样使用前瞻和后视

System.out.println(Arrays.toString("System.out.println(5.55);".split("(?<=\\D)\\.(?=\\D)")));

For further reference what they actually do you could read through this为了进一步参考他们实际上做了什么,你可以通读这个

A lookbehind and lookahead for letters is probably what you want.字母的后视和前瞻可能是您想要的。 DEMO演示

(?<=[a-zA-Z])\.(?=[a-zA-Z])

String content = "asdf.qweflkjasdf.qweflasdfasfd55.523";
Pattern p = Pattern.compile("(?<=[a-zA-Z])\\.(?=[a-zA-Z])");           
System.out.println(p.matcher(content).replaceAll("\n"));

OUTPUT:
asdf
qweflkjasdf
qweflasdfasfd55.523

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM