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

Use regex with group

Explorer ,
Nov 12, 2020 Nov 12, 2020

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, 

TOPICS
Actions and scripting , Windows
426
Translate
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

correct answers 1 Correct answer

Community Expert , Nov 12, 2020 Nov 12, 2020

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)
...
Translate
Adobe
Explorer ,
Nov 12, 2020 Nov 12, 2020

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)

 

Translate
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 ,
Nov 12, 2020 Nov 12, 2020

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

Translate
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 12, 2020 Nov 12, 2020

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

Translate
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 12, 2020 Nov 12, 2020
LATEST

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

 

Translate
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