[英]split on integer values but not floating point values
I have a java program where I need to split on integer values but not floating point values 我有一个Java程序,需要在整数值上拆分,但在浮点值上拆分
ie. 即。
"1/\\\\2"
should produce: [1,/\\\\,2]
but "1.0/\\\\2.0"
should produce: [1.0,/\\\\,2.0]
"1/\\\\2"
应产生: [1,/\\\\,2]
但"1.0/\\\\2.0"
应产生: [1.0,/\\\\,2.0]
does anybody have any ideas? 有人有什么想法吗?
or could anybody point me in the direction of how to split on the specific strings "\\\\/"
and "/\\\\"
? 还是有人可以指出我如何分割特定字符串
"\\\\/"
和"/\\\\"
?
UPDATE: sorry! 更新:对不起! one more case!
还有一种情况! for the string "100 /\\ 3.4e+45" I need to split it into: [100,/\\,3.4,e,+,45]
对于字符串“ 100 / \\ 3.4e + 45”,我需要将其拆分为:[100,/ \\,3.4,e,+,45]
my current regex is (kind of really ugly): 我当前的正则表达式是(真的很丑):
line.split("\\s+|(?<=[-+])|(?=[-+])|(?:(?<=[0-9])(?![0-9.]|$))|(?:(?<![0-9.]|^)(?=[0-9]))|(?<=[-+()])|(?=[-+()])|(?<=e)|(?=e)");
and for the string: "100 /\\ 3.4e+45" is giving me: [100,/\\,3.4,+,45] 对于字符串:“ 100 / \\ 3.4e + 45”给了我:[100,/ \\,3.4,+,45]
You could try something like this: 您可以尝试这样的事情:
String regex = "\\d+(.\\d+)?", str = "1//2";
Matcher m = Pattern.compile(regex).matcher(str);
ArrayList<String> list = new ArrayList<String>();
int index = 0;
for(index = 0 ; m.find() ; index = m.end()) {
if(index != m.start()) list.add(str.substring(index, m.start()));
list.add(str.substring(m.start(), m.end()));
}
list.add(str.substring(index));
The idea is to find number using regex
and Matcher
, and also add the strings in between. 这个想法是使用
regex
和Matcher
查找数字,并在两者之间添加字符串。
This regex should do it: 这个正则表达式应该做到这一点:
(?:(?<=[0-9])(?![0-9.]|$))|(?:(?<![0-9.]|^)(?=[0-9]))
It's two checks, basically matching: 这是两个检查,基本上匹配:
It will match the empty space after/before the digit, so you can use this regex in split()
. 它会与数字前后的空白匹配,因此您可以在
split()
使用此正则表达式。
See regex101 for demo. 演示请参见regex101 。
Follow-up 跟进
could anybody point me in the direction of how to split on the specific strings "\\/" and "/\\""
有人可以指出我如何分割特定字符串“ \\ /”和“ / \\””的方向
If you want to split before a specific pattern, use a positive lookahead : (?=xxx)
. 如果要在特定模式之前分割,请使用正向前瞻 :
(?=xxx)
。 If you want to split after a specific pattern, use a positive lookbehind : (?<=xxx)
. 如果要在特定模式后拆分,请使用正向后视 :
(?<=xxx)
。 To do either, separate by |
为此,请以
|
分隔 : :
(?<=xxx)|(?=xxx)
where xxx
is the text \\/
or /\\
, ie the regex \\\\/|/\\\\
, and doubling for Java string literal: 其中
xxx
是文本 \\/
或/\\
,即正则表达式\\\\/|/\\\\
,并且是Java字符串文字的两倍:
"(?<=\\\\/|/\\\\)|(?=\\\\/|/\\\\)"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.