如果第一个字符是减号,那么将其视为负号。

huangapple go评论80阅读模式
英文:

String split if the first character is minus then tread it as a negative sign

问题

- sign可以被视为运算符或负号。如果`-`位于开头,则应将其视为负号,并进行字符串中的减法运算。这仅适用于`-`号,而`+`号始终表示加号。如何实现这一点?

输入:

    -23-23+4=F1Qa;
    +23-23+4=F1Qa;

输出:

    ["-23","-","23","+","4","=","F","1","Q","a",";"]
    ["+", "23","-","23","+","4","=","F","1","Q","a",";"]

这是我目前尝试过的内容:

    String regx = "(?:# .*? #:? )|(?!^)(?=\\D)|(?<=\\D)(?=\\d-)"
    String[] splits = inputString.split(regx);
英文:

- sign can be treated as operator or negative sign. If - is located in the start, it shall be treated as the negative sign and a subtraction whiting the string. This is only apply to - sign while the + will be always the add sign. How can I achieve this?

input:

-23-23+4=F1Qa;
+23-23+4=F1Qa;

output:

[&quot;-23&quot;,&quot;-&quot;,&quot;23&quot;,&quot;+&quot;,&quot;4&quot;,&quot;=&quot;,&quot;F&quot;,&quot;1&quot;,&quot;Q&quot;,&quot;a&quot;,&quot;;&quot;]
[&quot;+&quot;, &quot;23&quot;,&quot;-&quot;,&quot;23&quot;,&quot;+&quot;,&quot;4&quot;,&quot;=&quot;,&quot;F&quot;,&quot;1&quot;,&quot;Q&quot;,&quot;a&quot;,&quot;;&quot;]

This is what I've tried so far

String regx = (?:# .*? #:?)|(?!^)(?=\\D)|(?&lt;=\\D)(?=\\d-)
String[] splits = inputString.split(regx);

答案1

得分: 1

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

public static void main(String[] args) {
    String str = "-23-23+4=F1Qa;&";
    Pattern pattern = Pattern.compile("^-\\d+|\\d+|\\D");
    Matcher matcher = pattern.matcher(str);
    while (matcher.find()) {
        System.out.println(matcher.group());
    }
}

}
Output:

-23

23
+
4

F
1
Q
a
;

Another test:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

public static void main(String[] args) {
    String str = "+23-23+4=F1Qa;&";
    Pattern pattern = Pattern.compile("^-\\d+|\\d+|\\D");
    Matcher matcher = pattern.matcher(str);
    while (matcher.find()) {
        System.out.println(matcher.group());
    }
}

}
Output:

23

23
+
4

F
1
Q
a
;

英文:

You can use the regex, ^-\d+|\d+|\D which means either negative integer in the beginning (i.e. ^-\d+) or digits (i.e. \d+) or a non-digit (\D).

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

	public static void main(String[] args) {
		String str = &quot;-23-23+4=F1Qa;&quot;;
		Pattern pattern = Pattern.compile(&quot;^-\\d+|\\d+|\\D&quot;);
		Matcher matcher = pattern.matcher(str);
		while (matcher.find()) {
			System.out.println(matcher.group());
		}
	}
}

Output:

-23
-
23
+
4
=
F
1
Q
a
;

Another test:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

	public static void main(String[] args) {
		String str = &quot;+23-23+4=F1Qa;&quot;;
		Pattern pattern = Pattern.compile(&quot;^-\\d+|\\d+|\\D&quot;);
		Matcher matcher = pattern.matcher(str);
		while (matcher.find()) {
			System.out.println(matcher.group());
		}
	}
}

Output:

+
23
-
23
+
4
=
F
1
Q
a
;

huangapple
  • 本文由 发表于 2020年10月19日 05:34:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64418574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定