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

Need help with regex

Community Beginner ,
Feb 18, 2019 Feb 18, 2019

Copy link to clipboard

Copied

I am working on a script to apply a hypertext marker to text with a specific character tag (Framemaker 19). The script will find the text with the  tag, read the text, and apply the marker. The problem is that it applies the marker to any text with this character tag and I need it to only apply to certain text (F1, T2, etc.). This is why I'm trying to use regex, but it's not working. My script and notes are below. I'm been staring at this way too long. Can anyone see what I'm doing wrong?

//This first part finds text that has the character tag "Link." (I know it needs to be a function, but I'm having trouble with that, too, so I'll deal with it later.)

var doc = app.ActiveDoc;

var charFmt = doc.GetNamedCharFmt("Link");

var findParams = AllocatePropVals(2);

var fmtName = "Link";

var pgf = doc.FirstPgfInDoc;

   

        findParams[0].propIdent.num = Constants.FS_FindCharTag;   

        findParams[0].propVal.valType = Constants.FT_String;   

        findParams[0].propVal.sval = fmtName; 

     

        findParams[1].propIdent.num = Constants.FS_FindWrap;   

        findParams[1].propVal.valType = Constants.FT_Integer;   

        findParams[1].propVal.ival = false; 

 

var textRange = new TextRange();

      textRange.beg.obj = textRange.end.obj = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf; 

      textRange.beg.offset = textRange.end.offset = 0; 

      doc.TextSelection = textRange;

      textRange = doc.Find(textRange.beg, findParams);

  

// This calls a function to read the text

var num = getText(pgf, doc);

     alert (num); // This works. It is getting and reading the text

// This is where I'm trying to use regex -- to match the variable "num"

var regex = /(?:Figure|Table) ((AF|AT|[FT])(\d+))/g;  // Match figure or table number in text. Would there be a way to add this to the findParams section instead?

if (regex.test (num) === true) { //This is what doesn't work for me

    createMarker (num);

    }

// If I just call the function "createMarker (num)" without using the regex, it will apply the correct marker, but to whatever text is tagged "Link." So if the word "few" is tagged "Link", it will have the marker "gotolink few," which is not what we need.

function getText (pgf, doc) {    //This works, reads the text

    var text = "", textItems, i; 

    if (textRange.constructor.name !== "TextRange") {  // Get a list of the strings in the text object or text range. 

        textItems = textRange.GetText(Constants.FTI_String); 

    } else { 

         textItems = doc.GetTextForRange(textRange, Constants.FTI_String); 

    } 

    // Concatenate the strings. 

    for (i = 0; i < textItems.len; i += 1) { 

        text += (textItems.sdata); 

    } 

return text;

}

function createMarker (text) { // This works, creates the marker

    var marker = doc.NewAnchoredObject(Constants.FO_Marker, (textRange.beg, textRange.end)); 

    var markerType = doc.GetNamedObject(Constants.FO_MarkerType, "Hypertext"); 

    marker.MarkerTypeId = markerType; 

    marker.MarkerText = "gotolink " + text.toLowerCase (); 

    return 1;

}

//Once I can get the regex working, the next step will be to make it search for the next instance of "Link," and then work its way through the document.

Thank you.

TOPICS
Scripting

Views

915

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

Advocate , Feb 18, 2019 Feb 18, 2019

I have only used regex in the match method for a string. I am not aware of the test method for a regex and not sure if that is supported in ExtendScript. Also, the regex support in ESTK is not extensive (pun intended) so you should not use the lookahead or the \d - I tested and they do not give me any matches.

Try this instead:

if ( mystring.match( '(Figure|Table) (AF|AT|FT)[0-9]+' ) != null ) createMarker( num );

Good luck

4everJang

Votes

Translate

Translate
Advocate ,
Feb 18, 2019 Feb 18, 2019

Copy link to clipboard

Copied

I have only used regex in the match method for a string. I am not aware of the test method for a regex and not sure if that is supported in ExtendScript. Also, the regex support in ESTK is not extensive (pun intended) so you should not use the lookahead or the \d - I tested and they do not give me any matches.

Try this instead:

if ( mystring.match( '(Figure|Table) (AF|AT|FT)[0-9]+' ) != null ) createMarker( num );

Good luck

4everJang

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 Beginner ,
Feb 19, 2019 Feb 19, 2019

Copy link to clipboard

Copied

That did it! Thank you!

if (num.match('(AF|AT|[FT])([0-9]+)') != null ) {

    createMarker (num);

    }

(Realized I didn't need the words Figure or Table)

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 19, 2019 Feb 19, 2019

Copy link to clipboard

Copied

I don't have a lot of time to help this morning, but I question the approach you are taking. I would try to find the strings first, and not the Link character tag. Can we assume that any text that matches the regular expression should become a link? Typically, I would do something like this:

1) Find the strings that need to be links. Each string will be a TextRange object somewhere in your code.

2) For each string, add the correct marker to the TextRange.

3) Extend the TextRange so that the new marker is included.

4) Apply the Link character format to the TextRange.

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
Advocate ,
Feb 19, 2019 Feb 19, 2019

Copy link to clipboard

Copied

It depends on the current state of the content. If there are Link character tags already, the approach works fine.

If there are no such tags or they might not be complete or consistent, Rick's approach would work.

But in such cases you should be really careful about your regex, as it might catch unwanted pieces of text and apply Link tags on them. I would then use the Figure or Table and whitespace in the regex to make it as specific as possible. And as I mentioned in another thread today (about replacing the ".gif" of images with ".png", there is no support for lookahead or lookbehind in the regex for ESTK.

Make sure to do thorough testing before you run an automated script. In my 15 years of scripting I found that about 75-80% of my time is spent on catching all possible errors.

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 Beginner ,
Feb 19, 2019 Feb 19, 2019

Copy link to clipboard

Copied

LATEST

The character tag Link is already there; I'm just adding the hypertext markers to them. However, the word Figure or Table isn't always included, so I can only search for F1 or T2, etc. This approach works great, at least for finding the first one.

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