Copy link to clipboard
Copied
Hi all,
I have the following Regular Expresion I'm running in InDesign to compare layer names to certain tagging. The script is freezing on this string:
var sizerx = /^(Y|W)?\d?X?(L|M|S|T)( |-)+?(Master)?$/g;
var str = "Women's 1/4 Zipper Guidelines - DO NOT TOUCH";
alert(sizerx.test(str));
I'm checking the layers to see if they match the following kinds of codes, all of which return true with the above regex:
I've tried stripping out the slash marks and apostrophes before testing with:
.replace(/[^a-zA-Z0-9\- \.]/gi,"");
To no avail. Is my regex too greedy? A more elegant way to write it? I will admit to regex not being my strong suit, but standard ECMA6 regex finders online successfully return false for the given str variable above. Thanks in advance.
Hi @brianp311 how about this? A slightly different approach. I moved a bit of cleanup to a last minute .replace() before testing the regex.
var sizerx = /^(\d?[XY]?[LMST])(\sMaster)?/;
var tests = [
'3XS - Master',
'2XS',
'XL Master',
'YM - Master',
'Women\'s 1/4 Zipper Guidelines - DO NOT TOUCH'
];
for (var i = 0; i < tests.length; i++)
$.writeln(sizerx.test(tests[i].replace(/[ \-]+/g, ' ')) + ': ' + tests[i]);
- Mark
As @m1b used [] instead of (), the following regex worked for me
/^[YW]?\d?X?[LMST][ |-]+?(Master)?$/g
But the point still remains to identify what is causing the hang.
-Manan
Copy link to clipboard
Copied
Hi @brianp311,
This is interesting, if it test it on online regex evaluators I don't see any suspect backtracking or anything suspecting. But I confirm that ESTK does hang. I don't think this is a case of catastrophic backtracking, this is something else probably a ExtendScript issue
-Manan
Copy link to clipboard
Copied
As @m1b used [] instead of (), the following regex worked for me
/^[YW]?\d?X?[LMST][ |-]+?(Master)?$/g
But the point still remains to identify what is causing the hang.
-Manan
Copy link to clipboard
Copied
Hi @brianp311 how about this? A slightly different approach. I moved a bit of cleanup to a last minute .replace() before testing the regex.
var sizerx = /^(\d?[XY]?[LMST])(\sMaster)?/;
var tests = [
'3XS - Master',
'2XS',
'XL Master',
'YM - Master',
'Women\'s 1/4 Zipper Guidelines - DO NOT TOUCH'
];
for (var i = 0; i < tests.length; i++)
$.writeln(sizerx.test(tests[i].replace(/[ \-]+/g, ' ')) + ': ' + tests[i]);
- Mark
Copy link to clipboard
Copied
Thank you both. Substituting brackets solved the issue. A curious case indeed.
Copy link to clipboard
Copied
Ok, so I have arrived at a simplied regex exhibiting the issue. Seems like using ? with a group is somehow causing the regex engine to go into an endless loop. The following hangs as well. Let's hope @Peter Kahrel has some insights to share on this
var sizerx = /^(Y|W)?a/g;
var str = "Wo";
alert(sizerx.test(str));
-Manan
Copy link to clipboard
Copied
Must be an ExtendScript bug. Its regex engine has several of them.
Copy link to clipboard
Copied
Hi @brianp311 ,
also see into this old thread from 2011:
Regular Expressions in CS5.5 - something is wrong
inspiria_si, Nov 03, 2011
https://community.adobe.com/t5/indesign-discussions/regular-expressions-in-cs5-5-something-is-wrong/...
Quoting the OP:
The third example - how to freeze ID 5.5 in one line (I posted it earlier in Photoshop forum because similiar problem was called earlier):
alert((/(n|s)? /gmi).test('s') );
It still will freeze InDesign 2022 when e.g. run from the Scripts panel.
Just tested it with 17.3.0 on Windows 10.
Regards,
Uwe Laubender
( Adobe Community Professional )
Copy link to clipboard
Copied
Hi all,
See also https://www.indiscripts.com/post/2022/01/the-hard-Problem-of-quantified-alternatives-extendscript
Best,
Marc
Copy link to clipboard
Copied
Thanks @Marc Autret and @Laubender for confirming what we suspected.
-Manan