Skip to main content
Inspiring
October 30, 2024
Answered

Help with RegExp to match and capture Foot-Inch.

  • October 30, 2024
  • 2 replies
  • 485 views

I'm writing a script to convert any text from foot-inch to just inches. I am pulling my hair out trying to get a RegExp that will match all my corner cases as well as capture the correct info. I need to capture the Feet portion (if present), the inches portion (if present) and the fractional inches portion (if present). Also, these may be preceeded or followed by other text in the same line. I want to use the same RegExp pattern to do the replacement using String.replace().

Are there any RegExp gurus out there that can help? Here are all the cases I need to deal with (spaces are indicated with a "•"):

 

 

13'-•2"
13'•-2"
13'•-•2"
13'•2"
13'2"
13'-2"
8'•6•3/4"
8'6•3/4"
8'-•6•3/4"
8'-6•3/4"
8'•-6•3/4"
8'•-•6•3/4"
7'•1/2"
7'1/2"
7'-•1/2"
7'-1/2"
7'•-1/2"
7'•-•1/2"
4'
3/8"
3•3/8"
14"

 

 

It would be nice if the group numbers could match across each case (i.e. feet are always group 1), but they don't need to be the same number so long as I can logic out which is which by the number of groups or which groups are undefined.

This topic has been closed for replies.
Correct answer jsavage77

I figured it out. This was deceptively complicated. I think in part because ExtendScript RegExp seems to have some bugs when you start using positive or negative look aheads. ADOBE, please give scripting some TLC.  I had no problem using an online JS regexp building to create a pattern that worked inside the builder, but ES never matched or captured the same way.

 

Anyway, for anyone seeking this pattern in the future, here's what finally I finally worked out that works with all of my text samples shown in my OP.

/(\d+')? ?-? ?(\d*(?!\/)) ?(\b\d+\/\d+\b)?"?/i

Group 1 will always match the feet (including the apostrophe) or will be undefined if there are no feet specified.

Group 2 will contain the whole inches or be empty if no whole inches

Group 3 will contain the fractional inches or be undefined if no fractional inches.

2 replies

jsavage77AuthorCorrect answer
Inspiring
October 31, 2024

I figured it out. This was deceptively complicated. I think in part because ExtendScript RegExp seems to have some bugs when you start using positive or negative look aheads. ADOBE, please give scripting some TLC.  I had no problem using an online JS regexp building to create a pattern that worked inside the builder, but ES never matched or captured the same way.

 

Anyway, for anyone seeking this pattern in the future, here's what finally I finally worked out that works with all of my text samples shown in my OP.

/(\d+')? ?-? ?(\d*(?!\/)) ?(\b\d+\/\d+\b)?"?/i

Group 1 will always match the feet (including the apostrophe) or will be undefined if there are no feet specified.

Group 2 will contain the whole inches or be empty if no whole inches

Group 3 will contain the fractional inches or be undefined if no fractional inches.

Vivek12
Community Manager
Community Manager
October 31, 2024
HI, Thank you for contacting us. Please try the script mentioned below: /(\d+)'?\s*-?\s*(\d+)?\s*(\d+\/\d+)?"/g It matches: Feet: (\d+)'? - digits followed by an optional '. Optional separator: \s*-?\s* - any spaces and optional hyphen. Inches: (\d+)? - digits, if present. Fractional inches: (\d+\/\d+)? - fractional format (e.g., 3/4), if present. If the issue persists, then please reply back to this thread, and one of our experts will assist you further. ^VS
jsavage77Author
Inspiring
October 31, 2024

That's close to what I came up with originally, but it doesn't match  8'6"  or    8'  .    It will match  3/4"  but does't capture the fraction. Also, FYI, the global flag doesn't allow capture in extendscript for some reason, so I left that off to test.