2

- 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:

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

This is what I've tried so far

String regx = (?:# .*? #:?)|(?!^)(?=\\D)|(?<=\\D)(?=\\d-)
String[] splits = inputString.split(regx);
SLN
  • 4,772
  • 2
  • 38
  • 79
  • What is the result from "what you're tried" and how is it different from what you expected? and - is this for some assignment? because this approach won't work for real, practical purposes. you won't be able to handle `3 + -5`, for example. To do that, you need to make an actual parser - it can't be handled by a regular expression. – Erwin Bolwidt Oct 18 '20 at 22:12

1 Answers1

1

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 = "-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
;
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Thanks a lot for the help. I have a complex regular expression used in the split and this is part of it. At the moment the code only allows to use the split method. Would be appropriate you could demonstrate using the string split function – SLN Oct 18 '20 at 21:59
  • For complex scenarios, Java regex API (`Pattern`, `Matcher` etc.) is less error-prone. There are many posts on SO e.g. [Java - Parsing strings - String.split() versus Pattern & Matcher](https://stackoverflow.com/questions/24813430/java-parsing-strings-string-split-versus-pattern-matcher) explaining this. – Arvind Kumar Avinash Oct 18 '20 at 22:09
  • You are right and thank for the suggestion, I will use Pattern Matcher, I think I get most of it from pattern marcher, would nice you could teach me how to remove a comments ?comments? The comments start with ? and space and the comments and closed by a space and ? again – SLN Oct 18 '20 at 22:16
  • @SLN - Removing something based on a regex is quite easy using `String#replaceAll`. However, I can provide a solution only after looking into the exact requirement. If this answer solved your problem, you can mark it as an answer and post a new question describing this new requirement. – Arvind Kumar Avinash Oct 18 '20 at 22:21
  • I'm about to go to bed but I can wait for 10 mins for your question. Nevertheless, SO is full of experts who can answer your question even if your question comes after I go to bed. – Arvind Kumar Avinash Oct 18 '20 at 22:25
  • 1
    I'm writing it now so appreciate – SLN Oct 18 '20 at 22:25
  • https://stackoverflow.com/questions/64418988/how-to-use-pattern-matcher-in-java-regex-api-to-remove-a-specific-line yes this is the question – SLN Oct 18 '20 at 22:35