[英]Regular expression for splitting a String while preserving whitespace
I am doing an Android project which needs to split a String into tokens while preserving whitespaces and also not to split at non-word characters like #
, &
etc ... 我正在做一个Android项目,该项目需要在保留空格的同时将String拆分为令牌,并且也不要在
#
, &
等非单词字符处拆分...
Using \\b
splits at any non-word character .So i need a way to split the string in the following way. 使用
\\b
在任何非单词字符处进行拆分。因此,我需要一种以下方式来拆分字符串。
Input: ( .
indicates whitespace) 输入:(
.
表示空格)
A.A#..A##
Desired output: 所需的输出:
A
.
A#
..
A##
So these 5 lines are the 5 values I would like in an array or similar. 因此,这5行是我希望在数组或类似数组中使用的5个值。 That means the 4th element of the result-array contains 2 spaces.
这意味着结果数组的第4个元素包含2个空格。
I think this is what you want: 我认为这是您想要的:
(?<=\S)(?=\s)|(?<=\s)(?=\S)
Basically I'm saying "if the previous character is a non-space and the next is a space or if the previous is a space and the next is a non-space, then split". 基本上,我是说“如果前一个字符是一个非空格,下一个是空格, 或者如果前一个是空格,而下一个是非空格,则拆分”。
Use StringTokenizer: 使用StringTokenizer:
StringTokenizer st = new StringTokenizer("A.A#..A##", ".");//first argument is string you want to split, another is whitespace
while(st.hasMoreTokens())
System.out.println(st.nextToken());
output will be: AA# A## 输出将是:AA#A ##
Try: 尝试:
String s = "AA#..A##";
if(s.contains("..")) | s.contains("...")) { s.replace("..", "."); s.replace("...", "."); String out[] = s.split(".");
It should give you an array with Strings the way you want :) 它应该以您想要的方式为您提供一个包含Strings的数组:)
Don't forget to replace the "." 不要忘记替换“。” with actual spaces :)
与实际空间:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.