Regex: Extract words separated by specific words

Viewed 124

These are input strings:
Line 1: item1 199-30, 1/1/2021
Line 2: item2 29 minus 5, 1/1/2021

I want to use regex to extract like this:
Line 1: 199, 30
Line 2: 29, 5

I tried, here is my regex:
([\d]+)[-|(\s*minus\s*)]([\d]+)

Only matched Line 1.

1 Answers

With your shown samples, please try following regex.

^item\d+\s+(\d+)(?:-|\s+minus)\s*(\d+)

Online demo for above regex

Explanation: Adding detailed explanation for above.

^item\d+\s+        ##Matching item from starting of value followed by 1 or more digits followed by 1 or more space occurrences.
(\d+)              ##Creating 1st capturing group which has digits in it.
(?:-|\s+minus)\s*  ##In a non-capturing group matching either - OR 1 or spaces followed by minus. Matching 0 or more spaces.
(\d+)              ##Creating 2nd capturing group which has digits in it.
Related