Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Matching strings between symbols

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • 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/ :

enter image description here

In this pattern:

  • \b asserts 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading