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

How to replace string while creating variable using GREP search pattern

Community Beginner ,
Dec 10, 2024 Dec 10, 2024

Copy link to clipboard

Copied

// constraints are:

// XX T will always be first and in caps ==> ^XX T

// T will always have a value of max of 2 digits ==> {1,2}

// L may or may not be present but if present will always have a value of max of 2 digits ==> {1,2}

// the digits of L if present will always be the end of the string ==> (/\d+$/)

 

var testName1 = "XX T3 L22";  // viable upper case search(/^XX T\d{1,2}( L\d{1,2}$)/g)

var testName2 = "XX T44 L1";

 

var testName3 = "XX T5";  // viable upper case search(/^XX T\d{1,2}$/g)

var testName4 = "XX T66;

 

 

// for testName1 ==> lNumber1 = 22

if (testName1.toString().search(/^XX T\d{1,2}( L\d{1,2}$)/g) === 0) lNumber1 = testName1.toString().replace((/^XX T\d{1,2}( L\d{1,2}$)/g), testName1.toString().match(/\d+$/));
 
// how can I return the T value in all 4 examples?
// results:
// lNumber1 = 3
// lNumber2 = 44
// lNumber3 = 5
// lNumber4 = 66
TOPICS
How-to , Scripting

Views

260

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 , Dec 10, 2024 Dec 10, 2024

Hi @nutradial, I assume you are doing this via ExtendScript... see if this helps. The match method of string is great for this.

- Mark

 

var tests = [
    "XX T3 L22",
    "XX T44 L1",
    "XX T5",
    "XX T66"
];

// match two digits of T and two digits of L (if L exists)
var matcher = /^XX T(\d{1,2})(?: L(\d{1,2}))?/;

for (var i = 0, T, L; i < tests.length; i++) {

    // perform the regex match
    var match = tests[i].match(matcher);

    if (!match)
        // no match at all
        conti
...

Votes

Translate

Translate
Adobe
Community Beginner ,
Dec 10, 2024 Dec 10, 2024

Copy link to clipboard

Copied

 

 

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 ,
Dec 10, 2024 Dec 10, 2024

Copy link to clipboard

Copied

Hi @nutradial, I assume you are doing this via ExtendScript... see if this helps. The match method of string is great for this.

- Mark

 

var tests = [
    "XX T3 L22",
    "XX T44 L1",
    "XX T5",
    "XX T66"
];

// match two digits of T and two digits of L (if L exists)
var matcher = /^XX T(\d{1,2})(?: L(\d{1,2}))?/;

for (var i = 0, T, L; i < tests.length; i++) {

    // perform the regex match
    var match = tests[i].match(matcher);

    if (!match)
        // no match at all
        continue;

    T = match.length > 0 ? Number(match[1]) : undefined;
    L = match.length > 1 ? Number(match[2]) : undefined;

    $.writeln('test string "' + tests[i] + '":  T = ' + T + '  L = ' + L);

}

 

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 ,
Dec 10, 2024 Dec 10, 2024

Copy link to clipboard

Copied

Nice!!! Thanks. 

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 ,
Dec 10, 2024 Dec 10, 2024

Copy link to clipboard

Copied

You're welcome!

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 ,
Dec 10, 2024 Dec 10, 2024

Copy link to clipboard

Copied

Can the match method as implemented above be used outside an object?

 

I am already iterating layer.name

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 ,
Dec 10, 2024 Dec 10, 2024

Copy link to clipboard

Copied

All you need is a String and a RegExp. You can call the "match" method of any String, passing a RegExp as parameter. Documentation is here. It returns null if no match, or an array where the zeroth element is the whole string, and subsequent elements are captured from the RegExp parentheses.

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 ,
Dec 11, 2024 Dec 11, 2024

Copy link to clipboard

Copied

To me the best part of this post is how you structured the regex to return the T and L value. I appreciate it ...I learned something very useful.

 

var regex = /^XX T(\d{1,2})(?: L(\d{1,2}))?/;
 

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 ,
Dec 11, 2024 Dec 11, 2024

Copy link to clipboard

Copied

Yes! Regex is the most amazing thing! Well worth taking the time to understand.

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 ,
Dec 11, 2024 Dec 11, 2024

Copy link to clipboard

Copied

This might be off topic and I'll gladly create a new thread if need be:

 

T = match.length > 1 ? Number(match[2]) : undefined;
 
When T is NaN in the match I would like to use it as follows but it fails to resolve:

if (layer.name !== "" && T == null) {    // this fails ==> while testing for NaN
 
if (layer.name !== "" && (!!T)) {    // this works ==> when T has value

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 ,
Dec 11, 2024 Dec 11, 2024

Copy link to clipboard

Copied

I was able to make it work using:

 

if ((!T) && layer.name !== "").   // for T is NaN

 

if ((!!T) && layer.name !== "").   // for T has value

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 ,
Dec 12, 2024 Dec 12, 2024

Copy link to clipboard

Copied

You can test for NaN ...

if (isNaN(T))

 

Also be careful about using  !T  this will return true if T is zero. When working with numbers it might be better to use something like if (undefined == T) . Depends on your specific case, but as a general rule I don't use that when working with numbers, only objects.

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 ,
Dec 14, 2024 Dec 14, 2024

Copy link to clipboard

Copied

I added a third parameter to my search pattern and was wondering if "named capturing groups" in ExtendScript are fully supported?

 

XX T7 L3 D0.125 ==>  Pattern match 1 (XX T7)  Pattern match 2 (L3)  Pattern match 3 (D0.125)

 

/(?<t>^XX T(\d{1,2}))?(?<l> ?L(\d{1,2}))?(?<d> ?D(\d*\.?\d{1,3}))?/g

 

Examples of matches:

XX T7

XX T7 L3

XX T7 D0.125

XX T7 L3 D0.125

XX T7 D0.125 L3

L3

D0.125

L3 D0.125

D0.125 L3

 

Named capturing groups:

t = 7

l = 3

d = 0.125

 

If named capturing groups are viable can you please update your initial snippet of code for reference. I am using the search and match method to trigger events based on layer names. This thread has not only been of high utility but very educational. Thanks.

 

 

 

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 ,
Dec 14, 2024 Dec 14, 2024

Copy link to clipboard

Copied

LATEST

Unfortunately ExtendScript RegExp doesn't support named capture groups. The best we can do is use a non-capturing group as I did in my original code, so that only the relevant patterns are matched..

 

/^XX T(\d{1,2})(?:\s?L(\d{1,2}))?(?:\s?D(\d*\.?\d{1,3}))?/

 

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