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

Regexp not picking up as expected

Engaged ,
Sep 18, 2018 Sep 18, 2018

Copy link to clipboard

Copied

I am running this regexp to move text frames containing certain words or characters to a layer called Hypertext. When I run the following code it is skipping some text frames that contain underscores. Can someone tell me what I am doing incorrectly here?

#target illustrator-22

var doc = app.activeDocument;

var allText = doc.textFrames;

var allLayers = doc.layers;

var check = 0;

var layerName = "Hypertext"

var newFillColor = new CMYKColor();

newFillColor.black = 0;

newFillColor.cyan = 0;

newFillColor.magenta = 100;

newFillColor.yellow = 100;

var titleBar = "Increase Font?";

var increaseFontPrompt = prompt("Do you want to increase the font size of text moved to the Hypertext layer for easy checking?\ny = yes\nn = no", "y", titleBar);

for (i = 0; i < allLayers.length; i++) {

    if (allLayers.name == "Hypertext") {

        check++;

    }

}

if (check == "0") {

    createAndName();

}

// create Hypertext layer

function createAndName() {

    var myLayer = allLayers.add();

    myLayer.name = layerName;

}

var targetLayer = doc.layers.getByName("Hypertext");

for (var i = allText.length - 1; i >= 0; i--) {

        var textFieldRange = allText.textRange;

/*   REGEXP of what to move and what to skip  */

    if (allText.contents.match(/_|(STARTER(?!\s))|BLOCK AS|(\W(SW(?![A-Z])))|HOURMETER|G7P|CD700|PM410|RELAY|KEYPAD/g) != null &&

    allText.contents.match(/SEAL_CAP|CBL_|_CABLE|THIS SCHEMATIC|SWEEP|FLOOD|FLD|LAMP|BEACON|(N.O.)|(N.C.)|(PIN)/g) == null) {

        // increase the font and make it red if it matches the regexp

        if (increaseFontPrompt == "y" | increaseFontPrompt == "Y" | increaseFontPrompt == "yes" | increaseFontPrompt == "Yes") {

        textFieldRange.characterAttributes.size = "14";

         textFieldRange.characterAttributes.fillColor = newFillColor;

        }

        allText.move(targetLayer, ElementPlacement.PLACEATEND);

    }

}

alert("Anything with an underscore has been moved \rto the Hypertext layer.\rReview and cleanup content accordingly. \rNot all components contain underscores and \rsome will need to be moved manually.");

Here is the actual file I am running it on. I have highlighted with yellow boxes the text frames that are not being picked up that I think should be.

https://drive.google.com/open?id=1NAppe5-rnwRdXIt4fnfthoXykOQM_Hf7

TOPICS
Scripting

Views

369

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 , Sep 18, 2018 Sep 18, 2018

Hi subieguy2​

first point:

(N.O.)|(N.C.)

should be

(N\.O\.)|(N\.C\.)

Than you will find three of the four missing words.

The dot means: any character. You have to escape the dot if you really will find a dot.

Other wise you do exclude INFORMATION and INFO_ and so on.

second point:

your second if/match contains (PIN)

That excludes the text RIPPER_PIN_PULLER_SW

Votes

Translate

Translate
Adobe
Community Expert ,
Sep 18, 2018 Sep 18, 2018

Copy link to clipboard

Copied

Hi subieguy2​

first point:

(N.O.)|(N.C.)

should be

(N\.O\.)|(N\.C\.)

Than you will find three of the four missing words.

The dot means: any character. You have to escape the dot if you really will find a dot.

Other wise you do exclude INFORMATION and INFO_ and so on.

second point:

your second if/match contains (PIN)

That excludes the text RIPPER_PIN_PULLER_SW

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
Engaged ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

The word PIN I am going to have to be ok with it excluding. There are too many other items that contain the word PIN that I do need to exclude in most other documents.

Here are examples:

IMPLEMENT SHUTOFF SW (PIN 6 POLE 2)

DATA SW (PIN 4 POLE 2)

I guess if there is a way to check and see if there is a ( before the word PIN that would work. Or if it is followed by a whitespace and a digit?

How would you write the regexp for that?

(\(PIN) - for the ( before the word PIN?

(PIN)\S\D - for the not a whitespace and then not a digit?

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 ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

LATEST

you can combine them:

… |N\.O\.|N\.C\.|\(PIN\s\d/g) == null)

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 ,
Sep 18, 2018 Sep 18, 2018

Copy link to clipboard

Copied

and one note:

you dont need four 'or s'

replace

if (increaseFontPrompt == "y" | increaseFontPrompt == "Y" | increaseFontPrompt == "yes" | increaseFontPrompt == "Yes") { 

with  

if (increaseFontPrompt.search (/y(es)?/i) != -1) {

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
Engaged ,
Sep 20, 2018 Sep 20, 2018

Copy link to clipboard

Copied

Thank you pixxxel schubser​!!!

I am still learning the ins and outs of regexp. I appreciate the help and feedback!

I will use your improvement on my for ors statement for sure. I have a similar script that I prompt the user for input and this is way better.

In this particular script I have changed from prompting the user for a yes or no input to allowing them to select if they want to increase the font, make the font red, both, or neither with check boxes. Here is the final script with your fixes included. Thank you again!

#target illustrator-22

var doc = app.activeDocument;

var allText = doc.textFrames;

var allLayers = doc.layers;

var check = 0;

var layerName = "Hypertext"

var newFillColor = new CMYKColor();

newFillColor.black = 0;

newFillColor.cyan = 0;

newFillColor.magenta = 100;

newFillColor.yellow = 100;

// Panel for checkboxes

var userPrefWindow = new Window('dialog', "Select your output options...");

// group the check boxes

var checkBoxGroup = userPrefWindow.add("group");

checkBoxGroup.orientation = "row";

// increase font check box

var changeSize = checkBoxGroup.add('checkbox', undefined, "\u00A0Increase Font", {

    justify: "center"

});

// make red check box

var changeToRed = checkBoxGroup.add('checkbox', undefined, "\u00A0Make Red");

// ok button

var buttons = userPrefWindow.add("group")

buttons.add("button", undefined, "OK", {

    name: "ok"

});

// cancel button

buttons.add("button", undefined, "Cancel", {

    name: "cancel"

});

// display the panel we just setup

userPrefWindow.show();

for (i = 0; i < allLayers.length; i++) {

    //alert(allLayers.name);

    if (allLayers.name == "Hypertext") {

        //alert("Found Hypertext");

        check++;

    }

}

if (check == "0") {

    createAndName();

}

// create Hypertext layer

function createAndName() {

    var myLayer = allLayers.add();

    myLayer.name = layerName;

}

var targetLayer = doc.layers.getByName("Hypertext");

for (var i = allText.length - 1; i >= 0; i--) {

    //for (var i = 0; i < allText.length; i++) {

    var textFieldRange = allText.textRange;

    // regexp of content to look for and content to ignore

    if (allText.contents.match(/_|(STARTER(?!\s))|BLOCK AS|(\W(SW(?![A-Z])))|HOURMETER|G7P|CD700|PM410|RELAY|KEYPAD/g) != null &&

        allText.contents.match(/SEAL_CAP|CBL_|_CABLE|THIS SCHEMATIC|SWEEP|FLOOD|FLD|LAMP|BEACON|(N\.O\.)|(N\.C\.)|(PIN)/g) == null) {

        // if red check box is selected, make it red

        if (changeToRed.value == true) {

            textFieldRange.characterAttributes.fillColor = newFillColor;

        }

        // if increase font size is selected, increase the font

        if (changeSize.value == true) {

            textFieldRange.characterAttributes.size = "14";

            //alert("Preliminary");

        }

        // move the text frame to the Hypertext layer

        allText.move(targetLayer, ElementPlacement.PLACEATEND);

        //thisTextFrame.contents = newString;

    }

}

alert("Anything with an underscore has been moved \rto the Hypertext layer.\rReview and cleanup content accordingly. \rNot all components contain underscores and \rsome will need to be moved manually.");

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