Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


How to extract parts of a filename using RegExp?

Adobe Community Professional ,
Aug 09, 2021 Aug 09, 2021

Copy link to clipboard

Copied

I don't use RegExp often, but in most cases I can handle it.

 

Now I have a list of filenames from cameras in the format
_MG_4991.cr2
DSC_0986.JPG

etc., in this case, between the digital index and the file extension there can be arbitrary characters (as a rule, the photographer's comments)

 

I want to parse these names into parts so that I get a separate prefix specific to the camera (for example, _MG_ or DSC_ or NEF_), the numerical value of the frame number and the file extension. At the same time, I need to ignore the comment of the photographer.

 

I read the RegExp documentation for a long time and ended up doing this:

 

var init = decodeURI(etTarget.path[i].name).match(new RegExp('([\D]*)(\d+)[\D]*(\.[\D]*)'))

 

(where etTarget.path [i] is an element of an array of objects of the File type). But it doesn't work.

I hope there are people here who are more advanced in working with strings and I will get an answer 🙂

TOPICS
Actions and scripting

Views

135

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
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Aug 09, 2021 Aug 09, 2021
f=['_MG_4991CommentTwo.cr2', 'DSC_0986CommentOne.JPEG', 'NEF_1234CommentThree.nef'], obj={} while(f.length)eval(f.shift().replace(/(\w{4}\d{4})(\w+)(\..{3,4}$)/i,'obj["$1$3"]="$2"'))f = ['_MG_4991Comment 1.cr2', 'DSC_0986Comment 2.JPEG', 'NEF_1234Comment 3.nef'], obj = {} while(f.length)eval(f.shift().replace(/(\w{4}\d{4})([\w ]*)(\..{3,4}$)/i,'obj["$1$3"]="$2"'))

Likes

Translate

Translate
Adobe Community Professional ,
Aug 09, 2021 Aug 09, 2021

Copy link to clipboard

Copied

 

f=['_MG_4991CommentTwo.cr2', 'DSC_0986CommentOne.JPEG', 'NEF_1234CommentThree.nef'], obj={}
while(f.length)eval(f.shift().replace(/(\w{4}\d{4})(\w+)(\..{3,4}$)/i,'obj["$1$3"]="$2"'))
f = ['_MG_4991Comment 1.cr2', 'DSC_0986Comment 2.JPEG', 'NEF_1234Comment 3.nef'], obj = {}
while(f.length)eval(f.shift().replace(/(\w{4}\d{4})([\w ]*)(\..{3,4}$)/i,'obj["$1$3"]="$2"'))

 

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
community guidelines
Adobe Community Professional ,
Aug 09, 2021 Aug 09, 2021

Copy link to clipboard

Copied

quote

 

f=['_MG_4991CommentTwo.cr2', 'DSC_0986CommentOne.JPEG', 'NEF_1234CommentThree.nef'], obj={}
while(f.length)eval(f.shift().replace(/(\w{4}\d{4})(\w+)(\..{3,4}$)/i,'obj["$1$3"]="$2"'))
f = ['_MG_4991Comment 1.cr2', 'DSC_0986Comment 2.JPEG', 'NEF_1234Comment 3.nef'], obj = {}
while(f.length)eval(f.shift().replace(/(\w{4}\d{4})([\w ]*)(\..{3,4}$)/i,'obj["$1$3"]="$2"'))

 


By @Kukurykus

 

messy again

f=['_MG_4991Comment Two.cr2', 'DSC_0986CommentOne.JPEG', 'NEF_1234CommentThree.nef'], obj={}
while(f.length)eval(f.shift().replace(/(\w{4}\d{4})(\w+)(\..{3,4}$)/i,'obj["$1$3"]="$2"'))

alert(obj.toSource())

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
community guidelines
Adobe Community Professional ,
Aug 09, 2021 Aug 09, 2021

Copy link to clipboard

Copied

LATEST

The first version was for comments with [a-z][0-9]_, another also for spaces.

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
community guidelines
Adobe Community Professional ,
Aug 09, 2021 Aug 09, 2021

Copy link to clipboard

Copied

var regExp = /([\D]*)(\d+)[\D]*(\.[\D]*)/,
init = regExp.exec(decodeURI(etTarget.path[0].name));

so it seems to work too

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
community guidelines
Adobe Community Professional ,
Aug 09, 2021 Aug 09, 2021

Copy link to clipboard

Copied

Yes, originally I kept your code, but then found it fails for some names so I created mine.

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
community guidelines
Adobe Community Professional ,
Aug 09, 2021 Aug 09, 2021

Copy link to clipboard

Copied

ГХМ!!!!

 

try {
fls=['AAA_1234 школа 10.jpg', 'DSC_0986Comment One.JPEG', 'NEF_1234CommentThree.nef'], obj={}
while(fls.length)eval(fls.shift().replace(/(\w{4}\d{4})(\w+)(\..{3,4}$)/i,'obj["$1$3"]="$2"'))

alert(obj.toSource())
} catch (e) { alert(e); }

var s = "AAA_1234 школа 10.jpg"

var regExp = /([\D]*)(\d+)[\D]*(\.[\D]*)/
init = regExp.exec(decodeURI(s));

alert(init.toSource())

 

???

 

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
community guidelines
Adobe Community Professional ,
Aug 09, 2021 Aug 09, 2021

Copy link to clipboard

Copied

I have a limited selection of possible comments and filenames. In any case, now I understand the approach and I can fix everything myself.

 

Меня больше удивляет, что в этом случае не работает match, я ожидал что он вернет тот же результат что exec. Понятно, что дело во мне, а не в match, но не пойму где ошибка

 

UPD. Понял. Кавычки были лишними 🙂

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
community guidelines
Adobe Community Professional ,
Aug 09, 2021 Aug 09, 2021

Copy link to clipboard

Copied

That was a standard approach covering scope of typical cases. That's not hard to improve that:

 

eval('AAA_1234 школа 10.jpg'.replace(/(\w{4}\d{4})([\w школа]*)(\..{3,4}$)/i,'({"$1$3":"$2"})')).toSource()

 

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
community guidelines
Adobe Community Professional ,
Aug 09, 2021 Aug 09, 2021

Copy link to clipboard

Copied

RegEx doesn't read foreign letters, but that is no problem. Either you include those letters in the code to be read or you'll use equivalent characters to gain same goal. There will be alike issue with characters from polish alphabet: ęóąśłżźćń

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
community guidelines