简体   繁体   中英

Regex for splitting specific operators such as (^,*, >,<,<=,>=, +, -, !, $)?

Trying to Create a Specific Method, that splits a formula into two arrays, an Array that holds the operators and the array that holds the numeric values.

Operators include: (^,*, >,<,<=,>=, +, -, !, $) Example: String formula = "17−(23+4)*5$"

Im currently using the String split function. I used the regex ("\\s*[^0-9]+\\s*"); and its working for the valueArray.

If someone can let me know what regex I should use to split the operators listed above, that'd be amazing.

To get the numeric values grouped as numbers you can use the regex:

(\s*\d+\s*)

Note that the ( and ) are part of the actual regex pattern itself.

To get the symbols you can use what is essentially just the inverse pattern

(\s*[^\d]\s*)

This would give you [−, (, +, ), *] . If you want the symbols grouped together then use

(\s*[^\d]\s*)

which will give you [−(, +, )*] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM