hitwill
hitwill

Reputation: 623

Capture words on the right side of | (OR) in regex expression that are not in the left

I am trying to capture words on the right side of this regex expression that are not captured on the left.

In the code below, the left side captures "17 inch" in this string: "this 235/45R17 is a 17 inch tyre"

(?<=([-.0-9]+(\s)(inches|inch)))|???????

However, anything I put in the right side, such as a simple +w is interfering with the left side

How can I tell the RegEx to capture any word, unless it is a digit followed by inch - in which case capture both 17 and inch?

Upvotes: 0

Views: 1230

Answers (1)

Ro Yo Mi
Ro Yo Mi

Reputation: 15000

Description

((?:(?![0-9.-]+\s*inch(?:es)?).)+)|([0-9.-]+\s*inch(?:es)?)

Regular expression visualization

** To see the image better, simply right click the image and select view in new window

Example

Live Demo

https://regex101.com/r/fY9jU5/2

Sample text

this 235/45R17 is a 17 inch tyre

Sample Matches

  • Capture group 1 will be the values that didn't match the 17 inch
  • Capture Group 2 will be the number of inches
MATCH 1
1.  [0-20]  `this 235/45R17 is a `

MATCH 2
2.  [20-27] `17 inch`

MATCH 3
1.  [27-32] ` tyre`

Explanation

NODE                     EXPLANATION
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    (?:                      group, but do not capture (1 or more
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
        [0-9.-]+                 any character of: '0' to '9', '.',
                                 '-' (1 or more times (matching the
                                 most amount possible))
----------------------------------------------------------------------
        \s*                      whitespace (\n, \r, \t, \f, and " ")
                                 (0 or more times (matching the most
                                 amount possible))
----------------------------------------------------------------------
        inch                     'inch'
----------------------------------------------------------------------
        (?:                      group, but do not capture (optional
                                 (matching the most amount
                                 possible)):
----------------------------------------------------------------------
          es                       'es'
----------------------------------------------------------------------
        )?                       end of grouping
----------------------------------------------------------------------
      )                        end of look-ahead
----------------------------------------------------------------------
      .                        any character except \n
----------------------------------------------------------------------
    )+                       end of grouping
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
 |                        OR
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    [0-9.-]+                 any character of: '0' to '9', '.', '-'
                             (1 or more times (matching the most
                             amount possible))
----------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
    inch                     'inch'
----------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
----------------------------------------------------------------------
      es                       'es'
----------------------------------------------------------------------
    )?                       end of grouping
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------

Upvotes: 1

Related Questions