• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Regular Expressions in CS5.5 - something is wrong

Community Beginner ,
Nov 03, 2011 Nov 03, 2011

Copy link to clipboard

Copied

Hello Everybody,

Please correct me, but I think, I found a serious problem with regular Expressions in Indesign CS5.5 (and possibly in other apps from CS5.5).

Let's start with simple example:

-------------

var range = "a-a,a,a-a,a";

var regEx = /(a+-a+|a+)(,(a+-a+|a+))*/;

alert( "Match:" +regEx.test(range)+"\nLeftContext: "+RegExp.leftContext+"\nRightContext: "+RegExp.rightContext );

-------------

What I expected was true match and the left  and the right context should be empty. In Indesign CS3 that is correct BUT NOT in CS5.5.

In CS 5.5 it seems that the only first "a-a" is matched and the rest is return as the rightContext - looks like big change (if not parsing error in RegExp engine).

Please correct me if I am wrong.

The second example - how to freeze ID CS5.5:

-------------

var range = "a-a,a,a-a,a";

var regEx = /(a+-a+|a+)(,(a+-a+|a+)){8,}/;

alert( "Match:" +regEx.test(range)+"\nLeftContext: "+RegExp.leftContext+"\nRightContext: "+RegExp.rightContext );

--------------

As you can see it differs only with the {8,} part instead of *

Run it in CS5.5 and you will see that the ID hangs (in CS3 of course it runs flawlessly}.

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') );

---------------

As you can guess - it freezes the CS5.5 (CS3 passes the test).

Please correct me if I am doing something wrong or it's the problem of Adobe.

Best regards,

Daniel Brylak

TOPICS
Scripting

Views

4.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 03, 2011 Nov 03, 2011

Copy link to clipboard

Copied

In reference to my previous message: After some digging, I think that the main problem is that regExp in CS5.5 works in non-greedy mode (despite the fact that the documentation says opposite)

I don't know any way to change it back to greedy mode.

After all it does not explain the possibility of freezing when executing some expressions.

I would be grateful if someone had any idea what am I doing wrong or maybe someone has workaround for that?

b.r.

Daniel

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 03, 2011 Nov 03, 2011

Copy link to clipboard

Copied

Hi Daniel,

Thanks for sharing. Really annoying indeed.

Just to complete your diagnosis, what you describe about CS.5 is the same in CS5, while CS4 behaves as CS3.

var range = "aaaaa";

var regEx = /(a+-a+|a+)(,(a+-a+|a+))*/;

alert([

    "Match:" +regEx.test(range),

    "LeftContext: "+RegExp.leftContext.toSource(),        // => CS3/4: EMPTY -- CS5+: EMPTY

    "RightContext: "+RegExp.rightContext.toSource()        // => CS3/4: EMPTY -- CS5+: ",a,a-a,a"

    ].join('\r'));

So there is a serious implementation problem of the RegExp object from ExtendScript CS5.

I don't think it's related to the greedy modes. By default, JS RegExp quantifiers are greedy, and /a*/ still entirely captures "aaaaaa" in CS5+.

By the way, you can make any quantifier non-greedy by adding ? after the quantifier, e.g.: /a*?/, /a+?/, etc.

I guess that Adobe ExtendScript has a generic issue in updating the RegExp.lastIndex property in certain contexts—see http://forums.adobe.com/message/3719879#3719879 —which could explain several bugs such as the Negative Class bug —see http://forums.adobe.com/message/3510078#3510078 — or the problems you are mentioning today.

@+

Marc

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 03, 2011 Nov 03, 2011

Copy link to clipboard

Copied

Hi Marc,

Thans for the complement of diagnosis. I recently upgraded from CS3 to CS5.5 and was not able to check the behaviour in CS4 and CS5.

Getting to the point, you are right that it's not the greedy mode problem - I've checked some other simple examples and sometimes worked as expeced, and sometimes suddenly wrong (freezing the execution).

Well then, I reported the bug to Adobe(and I suspect, I was not the first one)  - we will see what happens.

Best regards,

Daniel

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 03, 2011 Nov 03, 2011

Copy link to clipboard

Copied

I cannot be sure in whether it relates to this problem.

Please perform this sample code.

var str = "2011-11-03";
//var str = "11-03";
//var str = "03";
var regex = /^((\d+)-)?((\d+)-)?(\d+)$/;

$.writeln("Match : " + regex.test(str));
$.writeln("RegExp.$1 : " + RegExp.$1);
$.writeln("RegExp.$2 : " + RegExp.$2);
$.writeln("RegExp.$3 : " + RegExp.$3 );
$.writeln("RegExp.$4 : " + RegExp.$4 );
$.writeln("RegExp.$5 : " + RegExp.$5 );

I get:

Match : true
RegExp.$1 : 2011-
RegExp.$2 : 2011
RegExp.$3 : 11-
RegExp.$4 : 11
RegExp.$5 : 03

It's right!

But, If str = "11-03" then

Match : true
RegExp.$1 : 11-
RegExp.$2 : 11
RegExp.$3 : 
RegExp.$4 : 03
RegExp.$5 : 03

RegExp.$4 captured "03" why?

and, If str = "03" then

Match : true
RegExp.$1 : 
RegExp.$2 : 03
RegExp.$3 : 
RegExp.$4 : 03
RegExp.$5 : 03

RegExp.$2 captured "03"

I reported to Adobe site.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 03, 2011 Nov 03, 2011

Copy link to clipboard

Copied

Hi Seuzo,

I confirm the same results both in CS5.5 and CS3 (the versions I can check) - It leads to conclusion that CS3 implementation of RegExp was wrong too.

I believe that this is the same problem - just the guys from Adobe made a mess with the implementation

b.r

Daniel

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 04, 2011 Nov 04, 2011

Copy link to clipboard

Copied

Daniel, Seuzo,

Just posted a basic ScriptUI dialog to make regex-testing easier:

http://www.indiscripts.com/post/2011/11/extendscript-regexp-tester

@+

Marc

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 02, 2022 Aug 02, 2022

Copy link to clipboard

Copied

Hi @inspiria_si ,

about 11 years later this script will still freeze InDesign 2022 when run e.g. from the Scripts panel:

alert((/(n|s)? /gmi).test('s') );

Just tested with my German InDesign 2022 version 17.3.0 on Windows 10.

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 03, 2022 Aug 03, 2022

Copy link to clipboard

Copied

Sorry to hear.

This case cannot be denied one thing - the behavior is consistent across many versions 🙂

 

Personally, years ago I abandoned advanced jsx scripting in favor of a .net application in c# that uses the activeX control to control InDesign - I recommend it - it solved regular expressions and many other problems.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 03, 2022 Aug 03, 2022

Copy link to clipboard

Copied

LATEST

Good suggestion @inspiria_si, but that would limit one to just WIN platform. Probably using something like Python would be a better option(though I haven't tried that route personally.)

-Manan

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines