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

Regex to remove pattern from sequence

Guide ,
Mar 22, 2024 Mar 22, 2024

Copy link to clipboard

Copied

Hi,

 

I have a sequence of strings seperated by a comma.

 

1x,2x,1y,2y,1aa,2aa,1bb,1cc,2bb,2cc

 

I want to remove all of the strings starting with 2.  So my string would look like this:

1x,1y,1aa,1bb,1cc

 

I want to maintain the comma delimiter. Note the end of the sequence does not have a comma.

Currently I am using:

 

var s = "1j,2j,1vv,2vv,2xx,1N,2Y,1abc";

s = ( s.replace ( "2[^,]*", "", "g"));

alert ( s );

 

But I get left with double comma, which I clean up later.

 

Any suggestions for a better, faster solution, would be very welocome.

 

Thanks.

 

P.

 

TOPICS
Scripting

Views

142

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 , Mar 22, 2024 Mar 22, 2024

Hi @Pickory, not a huge improvement but this is how I would do it:

var s = '2x,1y,2y,1aa,2aa,1bb,1cc,2bb,2cc'
    .replace(/(^|,)2[^,]*/g, '')
    .replace(/^,/, '');

$.writeln(s);

 The 2nd replace is needed for when the "2" is found at the start.

- Mark

Votes

Translate

Translate
Community Expert ,
Mar 22, 2024 Mar 22, 2024

Copy link to clipboard

Copied

Hi @Pickory, not a huge improvement but this is how I would do it:

var s = '2x,1y,2y,1aa,2aa,1bb,1cc,2bb,2cc'
    .replace(/(^|,)2[^,]*/g, '')
    .replace(/^,/, '');

$.writeln(s);

 The 2nd replace is needed for when the "2" is found at the start.

- Mark

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
Guide ,
Mar 23, 2024 Mar 23, 2024

Copy link to clipboard

Copied

Thank you all for looking.

 

I think I will go with the one from Mark.

 

P.

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
Guide ,
Mar 25, 2024 Mar 25, 2024

Copy link to clipboard

Copied

Hi Pickory,

 

Definitely just for fun:

s = ((','+s).match(/,[^2][^,]*/g)||[]).join('').slice(1);

😉

 

Best,

Marc

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 ,
Mar 25, 2024 Mar 25, 2024

Copy link to clipboard

Copied

LATEST

Not only fun, but actually a logical way to approach it!

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 ,
Mar 23, 2024 Mar 23, 2024

Copy link to clipboard

Copied

I just tried a GREP search didn't do the script 

 

but this works for me in GREP

,(?<=,)2\w+

 

Change all seems to work

EugeneTyson_0-1711185859835.png

 

EugeneTyson_1-1711185866675.png

 

 

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 ,
Mar 23, 2024 Mar 23, 2024

Copy link to clipboard

Copied

Hi @Eugene Tyson, yes that's how you would do it via the UI, but you can't use lookbehinds in ExtendScript RegExp.

- Mark

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 ,
Mar 23, 2024 Mar 23, 2024

Copy link to clipboard

Copied

Ah yes - I see that - just wanted to put the GREP up there in case it was useful 😄

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