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

Illustrator Regular Expression - Negative Lookbehind??

Community Beginner ,
May 25, 2021 May 25, 2021

Copy link to clipboard

Copied

I am trying to use regex to find any digits except if they are preceded by "_" underscore symbol.  If they are preceded by underscore it should not match.  I have gotten this to work in RegEx testing but it looks as though negative lookbehind is not supported: 

 

var reg = /(?<![_\d+])\d+/g;

 

Is there a way to accomplish this without negative lookbehind?

The regex above should return the digits in NAME12 but not NAME_12

 

Thanks in advance.

TOPICS
Scripting

Views

901

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 , May 25, 2021 May 25, 2021

how about removing "_digits" first? would that work for you?

 

var str = "NAME14 but not NAME_12 and NAME23";

var re = /\d+/g;

var arr = str.replace(/_\d+/g, '').match(re);

for (var a=0; a<arr.length; a++) {
    alert(arr[a]);
}

Votes

Translate

Translate
Adobe
Community Expert ,
May 25, 2021 May 25, 2021

Copy link to clipboard

Copied

how about removing "_digits" first? would that work for you?

 

var str = "NAME14 but not NAME_12 and NAME23";

var re = /\d+/g;

var arr = str.replace(/_\d+/g, '').match(re);

for (var a=0; a<arr.length; a++) {
    alert(arr[a]);
}

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 ,
May 26, 2021 May 26, 2021

Copy link to clipboard

Copied

Thank you for your reply.  This would work, however, I am using the full string later on so removing that isn't ideal.  I am using this regex to make changes (color) to this selected range (any number in the string not directly following an underscore, in this case the underscore is treated as a space).  The color change should only happen on numbers that are not preceded by "_".  There will be other changes that are made to the full string later on as well.  So far I have not been able to get anything to work in ExtendScript.  The negative lookbehind is exactly what I need the regex to do but is unsupported for Adobe.

 

Below is how two different examples should be after this regex + color change:

NAME12 but not NAME_12

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 ,
May 26, 2021 May 26, 2021

Copy link to clipboard

Copied

Carlos' answer doesn't preclude using the full string later. It doesn't update the string or change it in any way. It's creating a new variable and storing an updated string in that variable. The original string is left untouched so that it could be referenced again later.

 

Can you give a more thorough example of what your string(s) will look like? Is it a long string with lots of stuff in it that might have several instances of numbers that need to be reformatted (like carlos' example above)? Or is it a bunch of short strings that will only have one instance that needs to be reformatted (like a bunch of text frames that say "NAME12" and "NAME26" and "NAME_45")? This kind of information is very helpful in terms of determining a plan of action. 

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 ,
May 26, 2021 May 26, 2021

Copy link to clipboard

Copied

Additionally, are you working with a string in your code? Or are you working with the contents of a textFrame in illustrator and then changing characterAttributes of specific textRanges?

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 ,
May 26, 2021 May 26, 2021

Copy link to clipboard

Copied

LATEST

Thank you Carlos and others who provided solutions.  I was able to get this to work for my needs.  To answer some other questions, my target was textframe contents which are fed variable data from a spreadsheet.  Most common cases are a NAME with 1-3 DIGITS following.

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 ,
May 25, 2021 May 25, 2021

Copy link to clipboard

Copied

try this:

 

[^_]+[\d]{2}

 

that's any character that's not an underscore at least once, followed by 2 digits.

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 ,
May 26, 2021 May 26, 2021

Copy link to clipboard

Copied

Thank you for your reply.  Please see my response to Carlos above for hopefully a more detailed description.  This regex gives me the following with my example text:

 

NAME12  NAME_12

 

I am changing the color of this range then performing other changes to the full string.  This is what I am trying to acheive:

 

NAME12  NAME_12

 

Also note, there may be more than two digits.  Example:  NAME1234  NAME_1234

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
Valorous Hero ,
May 26, 2021 May 26, 2021

Copy link to clipboard

Copied

Use a non-capturing group:

 

  var str = "NAME_1234";
  var rx = /(?:[^_])+\d+/;
  alert(str.match(rx)); // "1234"

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 ,
May 26, 2021 May 26, 2021

Copy link to clipboard

Copied

that would be the incorrect output for this use case though. in your example str = "NAME_1234", the expected output of match() should be undefined. it should only return the digits when they're not preceeded by an underscore.

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 ,
May 26, 2021 May 26, 2021

Copy link to clipboard

Copied

this one seems to work great

 

var str = "PLAYER45449929402384"
var rx = /[^_\d]([\d]{1,})/;
var myMatch = str.match(rx);
if(myMatch && myMatch.length && myMatch.length > 1)
	alert(str.match(rx)[1]);
else
	alert("no match");

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
LEGEND ,
May 26, 2021 May 26, 2021

Copy link to clipboard

Copied

@Disposition_Dev, can you provide a sample script showing how this can be used in ExtendScript? I can make it work in Keyboard Maestro but not ExtendScript.

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 ,
May 26, 2021 May 26, 2021

Copy link to clipboard

Copied

the regular expression?

 

var myRegex = /[^_][\d]{1,}/;

var str = "Name12 NAME_12";

var arr = str.match(myRegex);

for(var x=0;x<arr.length;x++)

{

    alert(arr[x]);

}

 

Or let's say you just wanted to alert a true/false if a string matches that regex:

 

var myRegex = /[^_][\d]{1,}/;

var str = "NAME12";

var str2 = "NAME_12"

alert(myRegex.test(str); //true

alert(myRegex.test(str2); //false   woops, this doesn't work. it's gonna find that 2 at the end of the string and return true

 

let's try this instead:

 

var myRegex = /[^_\d][\d]{1,}/; //look for anything that's not an underscore or a digit, followed by at least one digit

var str2 = "NAME_12";

alert(myRegex.test(str2); //false

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