0

I'm having a heck of a time finding a regex that will pull dimensions from a string.

Here is what I have so far, its not really doing what I want:

((\d+\s*(\d+\d+|\d+)*)\s*[xX]\s*(\d+\s*(\d+\d+|\d+)*)\s*[xX]\s*(\d+\s*(\d+\d+|\d+)*)|(\d+\s*(\d+\d+|\d+)*)\s*[xX]\s*(\d+\s*(\d+\d+|\d+)*)| (\d+\s*(\d+\d+|\d+)*))


Here are some examples of what it pulls (bolded):

-16" x 1476' 80 GA. EQ Ultra-Premium Hand Wrap - Use as replacement for 18" x 1500' 80 Gauge (4/Case)
-48 x 60" Corrugated Sheet (250/Bale)
-MP Die Cut Divider 25 7/ 8 x 19 3/4 1000/bale
-Part "B" (3"x3x 1/2" charcoal foam) only - extra pieces

I'm looking for it grab do the following:

-16" x 1476' 80 GA. EQ Ultra-Premium Hand Wrap - Use as replacement for 18" x 1500' 80 Gauge (4/Case)
-48 x 60" Corrugated Sheet (250/Bale)
-MP Die Cut Divider 25 7/8 x 19 3/4 1000/bale
-Part "B" (3"x3 x 1/2" charcoal foam) only - extra pieces

Notice the regex is not catching the lower part of the fraction because of the "/", also if an inch symbol (i.e. ") is between that dimension and another number it won't grab the first number, you can see that in the first example.

Once I have this regex working, I can strip out the inch and foot symbols (i.e. " and '), and break each number down into each dimension. Just trying to pull the initial dimension numbers first.

Thanks so much if you have any input.

mgmedick
  • 636
  • 6
  • 18

2 Answers2

1

I don't understand the x-in-the-middle-of-the-number thing, and I didn't attempt to get that part, but otherwise, this works:

([0-9]+["']?(?: [0-9]+/[0-9]+)? x [0-9]+["']?(?: [0-9]+/[0-9]+)?)

Regular expression visualization

Debuggex Demo

Please consider bookmarking the Stack Overflow Regular Expressions FAQ for future reference.

Community
  • 1
  • 1
aliteralmind
  • 18,274
  • 16
  • 66
  • 102
1
(\d+\s*\d+\s*\/\d+|\d+\s*\/\d+|\d+)["']?(\s*[xX]\s*(\d+\s*\d+\s*\/\d+|\d+\s*\/\d+|\d+)["']?)+

Demo

Amadan
  • 169,219
  • 18
  • 195
  • 256