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

using regular expressions to remove part of a text stream

Participant ,
Jan 30, 2017 Jan 30, 2017

Copy link to clipboard

Copied

I've got a script to take a psd file, convert to CMYK, flatten it and save it out with "CMYK_flat.psd" on the end of the new file.

All of that works fine, but I often work on RGB files and sometimes name them with _RGB.psd so I can see everything at a glance.
I need to use expressions somehow to search for the exact phrase of *RGB* anywhere in the name and remove it.
so I dont end up with names like this: myfile_RGB_CMYK.psd

here are the relevant lines of codes that I'm using to get the document name and save it out, if it helps:

var docName = app.activeDocument.name.match(/(.*)(\.[^\.]+)/)[1];

and

var saveFile = File( CurrentPath + "/" + docName + "_CMYK_flat.psd");

I've tried a few things but don't really have a clue how to do this, so any help would be appreciated.

Thanks!

TOPICS
Actions and scripting

Views

841

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

Community Expert , Jan 30, 2017 Jan 30, 2017

spaciousmind,

one way with minimal changes in your code could be:

var docName = app.activeDocument.name.match(/(.*)(\.[^\.]+)/)[1].replace(/_RGB/i,"");

Have fun

Votes

Translate

Translate
Adobe
Community Expert ,
Jan 30, 2017 Jan 30, 2017

Copy link to clipboard

Copied

spaciousmind,

one way with minimal changes in your code could be:

var docName = app.activeDocument.name.match(/(.*)(\.[^\.]+)/)[1].replace(/_RGB/i,"");

Have fun

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
Participant ,
Jan 30, 2017 Jan 30, 2017

Copy link to clipboard

Copied

Awesome, this works perfectly.
Thanks pixxxel schubser!

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
Participant ,
Jan 30, 2017 Jan 30, 2017

Copy link to clipboard

Copied

actually, theres something else, how would I go about adding more strings to the argument to be excluded?
not really as crucial but would be good to cover all my bases so I don't get double ups.
for example"_flat" and "_CMYK".....  it would need to be the exact phrases again obviously

just not sure how to add them in there, I tried adding them each in seperate lines but that doesn't work
(I should have known each one would replace the other lol)

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 ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

I hope I understand you right.

It's possible to adding more strings to the argument to be excluded.

If you have files eg like:

12345_Rgb.tif and

34567_fLAt.jpeg

and the result should always be:

12345_CMYK.tif and

34567_CMYK.jpeg

you only have to add one mor replace method with the next string.

var docName = app.activeDocument.name.match(/(.*)(\.[^\.]+)/)[1].replace(/_rgb$/i,"").replace(/_flat$/i,"");

This is easy to write, but a little bit cumbersome.

The part in between slashes is a regular expression.

Better is to use the summarizing arguments /_rgb$/ and /_flat$/ in a new Regex --> /_(rgb|flat)$/

Now your new line should look like that

var docName = app.activeDocument.name.match(/(.*)(\.[^\.]+)/)[1].replace(/_(rgb|flat)$/i,"");

Have fun

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
Participant ,
Feb 01, 2017 Feb 01, 2017

Copy link to clipboard

Copied

thanks man, it's really good to have that little bit of explination explaining all this stuff. I tried looking up a page about reg ex but most of it wasn't really relevant and I nearly fell asleep at my keyboard before I found anything useful

this line works for me perfectly:

var docName = app.activeDocument.name.match(/(.*)(\.[^\.]+)/)[1].replace(/_flat/i,"").replace(/_RGB/i,"").replace(/_CMYK/i,"");

And it'll do fine for now, however, I couldn't get this one to work:
var docName = app.activeDocument.name.match(/(.*)(\.[^\.]+)/)[1].replace(/_(rgb|cmyk|flat)$/i,""); 

you're right it does look more elegant, would be nice if I could get it to work!
I tested it as many different ways I could think of, it seems to only catch the first one inside the brackets or something like that, in this example it would only replace the (_RGB)

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 ,
Feb 01, 2017 Feb 01, 2017

Copy link to clipboard

Copied

Hmmh?

Your second variant also should work. Can you post the full file names which are not catched with this Regex, please?

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
Participant ,
Feb 01, 2017 Feb 01, 2017

Copy link to clipboard

Copied

I tested it just now with this exact line.
var docName = app.activeDocument.name.match(/(.*)(\.[^\.]+)/)[1].replace(/_(rgb|cmyk|flat)$/i,""); 

I started with thing_RGB_flat.psd

and it came back with thing_RGB_CMYK_flat.psd

if I give it thing_RGB_CMYK_flat.psd
it comes back with thing_RGB_CMYK_CMYK_flat.psd

So in both examples it seems to have caught the flat but nothing else.....

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 ,
Feb 01, 2017 Feb 01, 2017

Copy link to clipboard

Copied

Hahaha,

I never thought, you want to use all variations in row in one name.

Try this

var docName = app.activeDocument.name.match(/(.*)(\.[^\.]+)/)[1].replace(/(_(rgb|cmyk|flat))+$/i,"");

One note: Regex ist a very wide wide area.

Have fun

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
Participant ,
Feb 02, 2017 Feb 02, 2017

Copy link to clipboard

Copied

LATEST

thanks man, this line works perfectly.
Really good to see both ways of doing it.....

You're right Regex is pretty huge and confusing....
This suits my purposes well so I'm not even gonna attempt to figure out everything that's going on in there

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