Highlighted

Use regex with group

Community Beginner ,
Nov 12, 2020

Copy link to clipboard

Copied

Hello,

 

I try to acheive regex matching with group but it won't work.
And what i read is in java it can't forward...but here is a way to do this ?

 

I am learning java, but i have good basics in python. Here is what I will have written in python :

re.fullmatch(r"(?P<prod>\w\d\w)_(?P<ep>\d{3})_(?P<sht>(\d{3}_\w)|(\d{3}))_(?P<tsk>(\w{5})|(\w{5}\S\w))_(?P<ver>\d{2}).(?P<ext>\w{3})",

Is for matching fileName like this :

PRO_100_010_A_BGCOL_01.psd
PRO_100_010_A_BGCOL_A_01.psd
PRO_100_010_BGCOL_01.psd
PRO_100_010_BGCOL_A_01.psd

you can test-it, it work :

https://regex101.com/

 

Even if i try to match (with no forward) only one :

PRO_100_010_A_BGCOL_01.psd

var str = "PRO_100_010_A_BGCOL_01.psd"
var matchA = /(?<prod>\w{3})_(?<ep>\d{3})_(?<sht>\d{3}_\w)_(?<tsk>\w{5})_(?<ver>\w{2}).(?<ext>\w{3})/g;
var groups = str.match(matchA).groups;
alert(groups.prod);
alert(groups.ep);
alert(groups.sht);

i've got error ?

 

regards, 

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

Seems like the named groups are not supported by Extendscript. Something like the following should work to check if the string matches or not

var str = "PRO_100_010_A_BGCOL_01.psd"
var matchA = /(\w{3})_(\d{3})_(\d{3}_\w)_(\w{5})_(\w{2}).(\w{3})/g;
var groups = str.match(matchA)
alert(groups)

If you want to get the match in the group, try the following code

 

var str = "PRO_100_010_A_BGCOL_01.psd"
var reg = new RegExp(/(\w{3})_(\d{3})_(\d{3}_\w)_(\w{5})_(\w{2}).(\w{3})/g);
var m = reg.exec(str)
alert(m[1]);
alert(m[2]);

-Manan

TOPICS
Actions and scripting, Windows

Views

50

Likes

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

Use regex with group

Community Beginner ,
Nov 12, 2020

Copy link to clipboard

Copied

Hello,

 

I try to acheive regex matching with group but it won't work.
And what i read is in java it can't forward...but here is a way to do this ?

 

I am learning java, but i have good basics in python. Here is what I will have written in python :

re.fullmatch(r"(?P<prod>\w\d\w)_(?P<ep>\d{3})_(?P<sht>(\d{3}_\w)|(\d{3}))_(?P<tsk>(\w{5})|(\w{5}\S\w))_(?P<ver>\d{2}).(?P<ext>\w{3})",

Is for matching fileName like this :

PRO_100_010_A_BGCOL_01.psd
PRO_100_010_A_BGCOL_A_01.psd
PRO_100_010_BGCOL_01.psd
PRO_100_010_BGCOL_A_01.psd

you can test-it, it work :

https://regex101.com/

 

Even if i try to match (with no forward) only one :

PRO_100_010_A_BGCOL_01.psd

var str = "PRO_100_010_A_BGCOL_01.psd"
var matchA = /(?<prod>\w{3})_(?<ep>\d{3})_(?<sht>\d{3}_\w)_(?<tsk>\w{5})_(?<ver>\w{2}).(?<ext>\w{3})/g;
var groups = str.match(matchA).groups;
alert(groups.prod);
alert(groups.ep);
alert(groups.sht);

i've got error ?

 

regards, 

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

Seems like the named groups are not supported by Extendscript. Something like the following should work to check if the string matches or not

var str = "PRO_100_010_A_BGCOL_01.psd"
var matchA = /(\w{3})_(\d{3})_(\d{3}_\w)_(\w{5})_(\w{2}).(\w{3})/g;
var groups = str.match(matchA)
alert(groups)

If you want to get the match in the group, try the following code

 

var str = "PRO_100_010_A_BGCOL_01.psd"
var reg = new RegExp(/(\w{3})_(\d{3})_(\d{3}_\w)_(\w{5})_(\w{2}).(\w{3})/g);
var m = reg.exec(str)
alert(m[1]);
alert(m[2]);

-Manan

TOPICS
Actions and scripting, Windows

Views

51

Likes

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
Nov 12, 2020 0
Community Beginner ,
Nov 12, 2020

Copy link to clipboard

Copied

Ok, try this way, but error :

//var documents = app.activeDocument;
//var docName = app.activeDocument.name;
//var docPath = app.activeDocument.path.fsName;
var docName = "F2R_405_017_COLOR_01.psd"
var matchA = /(?<prod>\w{3})_(?<ep>\d{3})_(?<sht>\d{3}_\w)_(?<tsk>\w{5})_(?<ver>\w{2}).(?<ext>\w{3})/g;
var groups = docName.match(new RegExp(matchA, "g").groups;
var ep = groups.ep;
alert(ep)

 

Likes

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
Reply
Loading...
Nov 12, 2020 0
Adobe Community Professional ,
Nov 12, 2020

Copy link to clipboard

Copied

Seems like the named groups are not supported by Extendscript. Something like the following should work to check if the string matches or not

var str = "PRO_100_010_A_BGCOL_01.psd"
var matchA = /(\w{3})_(\d{3})_(\d{3}_\w)_(\w{5})_(\w{2}).(\w{3})/g;
var groups = str.match(matchA)
alert(groups)

If you want to get the match in the group, try the following code

 

var str = "PRO_100_010_A_BGCOL_01.psd"
var reg = new RegExp(/(\w{3})_(\d{3})_(\d{3}_\w)_(\w{5})_(\w{2}).(\w{3})/g);
var m = reg.exec(str)
alert(m[1]);
alert(m[2]);

-Manan

Likes

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
Reply
Loading...
Nov 12, 2020 2
Community Beginner ,
Nov 12, 2020

Copy link to clipboard

Copied

Thank you, i'll try with (a|b) (match a or b) if it works

Likes

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
Reply
Loading...
Nov 12, 2020 0
Community Beginner ,
Nov 12, 2020

Copy link to clipboard

Copied

Nice ! @Manan Joshi 

Thank you again 😉

 

I figured out :

var match = new RegExp(/(\w\d\w)_(\d{3})_(\d{3}_\w|\d{3})_(\w{5}|\w{5}\S\w)_(\d{2}).(\w{3})/g);

 

Likes

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
Reply
Loading...
Nov 12, 2020 0