J
J
Jake Taylor2020-09-09 22:02:57
Java
Jake Taylor, 2020-09-09 22:02:57

What should I fix in the regex so that it separates signed numbers?

There is a line like this:
5+1/2*(2+5*2- 14))*1200

I made up a regular expression:
(?<=\D)(?=\d|\D)|(?<=\d|\D)(?=\D)
It splits the given string into numbers, brackets and arithmetic operators. As a result, we get:

Найдено математическое выражение: [(-5+1/2*(2+5*2- --j))*1200]
Символ: [(]
Символ: [5]
Символ: [+]
Символ: [1]
Символ: [/]
Символ: [2]
Символ: [*]
Символ: [(]
Символ: [2]
Символ: [+]
Символ: [5]
Символ: [*]
Символ: [2]
Символ: [-]
Символ: [14]
Символ: [)]
Символ: [)]
Символ: [*]
Символ: [1200]


But it doesn't quite fulfill its intended purpose. Now, if the number is signed (-5, for example), I need to include this same sign in the symbol too, so that it turns out like this: - 5+1/2*(2+5*2-14))*1200
and the output was :
Символ: [(]
Символ: [-5]      <- СМОТРИ СЮДА, ТУТ ЧИСЛО СО ЗНАКОМ, НЕЛЬЗЯ ОТДЕЛЯТЬ ЗНАК ОТ ЧИСЛА
Символ: [+]
Символ: [1]
Символ: [/]
Символ: [2]
Символ: [*]
Символ: [(]
Символ: [2]
Символ: [+]
Символ: [5]
Символ: [*]
Символ: [2]
Символ: [-]
Символ: [14]
Символ: [)]
Символ: [)]
Символ: [*]
Символ: [1200]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Mikhailov, 2020-09-09
@Psixodelik

Try to look at the area of ​​such a regular season:
(-?\d*\.{0,1}\d+)
She will choose any numbers. positive and negative

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question