There is a string, let's say "2.2 * 2 + 3/4 + 2". Is it possible to write such a regular expression which will allocate a substring of the type number * number and number / number? that is, what would result in String [] s = {"2.2 * 2", "3/4"}.

and what function to cut the original string? tried split does not come out. Perhaps this can be obtained by others in some other way, not through regular expressions?

I really hope for an answer! Thanks a lot in advance!

    1 answer 1

    import java.util.regex.*; import java.util.ArrayList; public class RegexDemo { public static void main(String[] args) { String text = "2.2*2+3/4+2"; Pattern p = Pattern.compile("\\d+(\\.\\d+)?(\\*|\\/)\\d+(\\.\\d+)?"); Matcher m = p.matcher(text); ArrayList<String> matches = new ArrayList<String>(); while(m.find()) { matches.add(m.group()); } } }