-1

I have the following strings

ALEXANDRITE OVAL 5.1x7.9 GIA# 6167482443 FINE w:1.16
ALEXANDRITE OVAL 4x6 FINE w:1.16

I want to match the 5.1 and 7.9 and the 4 and 6 and not w:1.16 or w: 1.16 or the 6167482443. So far I managed to come up with these:

Matching the w:1.16 w: 1.16

([w][:]\d\.?\d*|[w][:]\s?\d\.?\d*) 

Matching the other digits:

\d+\.?\d{,3}

I kind of expected this not the return the long number sequence because of the {,3} but it still does.

My questions are : 1. How do I combine the two patterns excluding one and returning the other? 2. How do I exclude the long sequence of numbers? Why is it not being excluded now?

Thanks!

Jursels
  • 173
  • 1
  • 3
  • 12

3 Answers3

2

You could simply use the below regex.

\b(\d+(?:\.\d+)?)x(\d+(?:\.\d+)?)

DEMO

Explanation:

\b                       the boundary between a word char (\w) and
                         something that is not a word char
(                        group and capture to \1:
  \d+                      digits (0-9) (1 or more times)
  (?:                      group, but do not capture (optional):
    \.                       '.'
    \d+                      digits (0-9) (1 or more times)
  )?                       end of grouping
)                        end of \1
x                        'x'
(                        group and capture to \2:
  \d+                      digits (0-9) (1 or more times)
  (?:                      group, but do not capture (optional):
    \.                       '.'
    \d+                      digits (0-9) (1 or more times)
  )?                       end of grouping
)                        end of \2
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
1
([\d\.])+x([\d\.])+

matches

5.1x7.9

4x6
nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
0
(\d+(?:\.\d+)?)(?=x)|(?<=x)(\d+(?:\.\d+)?)

You can try this.See demo.

http://regex101.com/r/wQ1oW3/6

2)To ignore the long string you have to use \b\d{1,3}\b to specify boundaries.

http://regex101.com/r/wQ1oW3/7

Or else a part of long string will match.

vks
  • 67,027
  • 10
  • 91
  • 124