It’s a few days I’m trying to solve this issue but I can’t make it work.
I have looked at many questions here on Stack-overflow but still I can’t figure out the correct way to solve it.
I have string representing arithmetic expressions with numbers and "multi word" variables such as (Car speed / Road-congestion Time) + 100 and I need to match everything but +-*/().
I need to get the following three matches:
- Car speed
- Road-congestion Time
- 100
The following expression kind of work (it adds an extra empty match at the end) [+-\\*\\/()]?([^+-\\*\\/()]*)[+-\\*\\/()]? for string without hyphens in the "multi word" variables. To distinguish minus signs and hyphens, the minus signs are always surrounded by spaces but I can’t figure out how to specify that in the regexp.
Any ideas how to update my regexp to fit all the cases?
As a plus, the regexp should ignore numbers (get only the first two matches in the example above).
PS – Splitting is the last option and if possible I’d like to use a regular expression.
>Solution :
You can try something like this:
\b(?:[a-zA-Z][-a-zA-Z0-9_ ]*|\d+)\b
This is the result on https://regex101.com/ :
In this pattern:
-
\basserts a word boundary to ensure that we are matching whole
words. -
(?: ... )is a non-capturing group to match either multi-word
variables or numbers. -
[a-zA-Z][-a-zA-Z0-9_ ]*matches multi-word variables.
It starts with a letter and can contain letters, numbers, hyphens,
underscores, and spaces. -
|is the alternation operator, allowing the pattern to match either
multi-word variables or numbers. -
\d+matches one or more digits for numbers.
