How to token an empty string after a word

I want to type "sin(35)" into a textfield, but to calculate it, I have to separate every operator and number with a space since I used .split(" "), how do I set the delimiter to be the empty string after a number or operator, so it can accept no spaces?

pseudocode: infix.split("" after sin | "" after [()+-*^])

Answers


If you are simply trying to use split to get the formula parameter, you could use the Pattern and Matcher classes instead, like so:

String function = "";
int parameter = 0;
Pattern pattern = Pattern.compile("(sin)\\((\\d+)\\)"); // Compile the regex pattern.
Matcher matcher = pattern.matcher("sin(35)");           // Instantiate a pattern Matcher to search the string.
while (matcher.find()) {                                // For every match...
    function = matcher.group(1);                        // Get group `$1`.
    String s = matcher.group(2);                        // Get group `$2`.
    parameter = Integer.parseInt(s);                    // Parse to int, throws `NumberFormatException` if $2 is not a number.
}
System.out.println(function);                           // Prints "sin".
System.out.println(parameter);                          // Prints 35.

Regex:

(sin)\((\d+)\)


You only need one line to extract each part:

String function = input.replaceAll("\\(.*", "");
String parameter = input.replaceAll(".*\\(|\\).*", "");

Need Your Help

static_if in C99's preprocessor

c c99 c-preprocessor static-if

Is it possible to implement static_if in C99?