Skip to main content
SasiKumarVVS
Inspiring
February 11, 2019
Answered

Use GREP to find \r or \n

  • February 11, 2019
  • 4 replies
  • 2598 views

Hi all,

          Consider a paragraph with multi lines, in between there is two lines seems empty. Now i have to break the paragraph based on the empty lines or carriage returns using GREP. Thanks for any help.

Regards,

Sasi

This topic has been closed for replies.
Correct answer Manan Joshi

Hi Sasi,

Try the following, it will alert all the values that match, which you can then write to a text file or use as needed. Change the prompt word to anything else if needed, mind that the [ and ] would need to be escaped as done below. The a.contents is the text content of one block of match

app.findGrepPreferences.findWhat = "\\[Small\\]\\r(.*[\\r\\n])+?(?=\\r\\r)";       

var match = app.activeDocument.findGrep();   

 

// Clear the find/change preferences after the search   

app.findGrepPreferences = NothingEnum.NOTHING;

while(a = match.pop())

     alert(a.contents)

-Manan

4 replies

SasiKumarVVS
Inspiring
February 13, 2019

Hi all,

Could you help me to find one more grep?  That is, If the user types in prompt like small or large and another prompt types some number if both are in the are in the same paragraph alert to user.   I tried the following grep. But that did not work perfectly.

(?i)\[small\]\r(.*[\r\n])+?(?=1234)(.*)+?(.*[\r\n])+?(?=\r\r)

Regards,

Sasi

spicyDoge
Known Participant
February 13, 2019

I'm taking a wild stab on this.....

find

.*?: *([^\v]+)\v*.*?: *([^\v])

replace

[$]\rFOO\rBAR\r$2\rbiz\rbaz

SasiKumarVVS
Inspiring
February 12, 2019

Thank you all for your replies.

Now the requirement is if the user types small in prompt, have to get all contents after the word and before the double break lines and export to a text file.I tried with regex but that did not worked.Thanks for any help.

Regards,

Sasi

Manan JoshiCorrect answer
Community Expert
February 12, 2019

Hi Sasi,

Try the following, it will alert all the values that match, which you can then write to a text file or use as needed. Change the prompt word to anything else if needed, mind that the [ and ] would need to be escaped as done below. The a.contents is the text content of one block of match

app.findGrepPreferences.findWhat = "\\[Small\\]\\r(.*[\\r\\n])+?(?=\\r\\r)";       

var match = app.activeDocument.findGrep();   

 

// Clear the find/change preferences after the search   

app.findGrepPreferences = NothingEnum.NOTHING;

while(a = match.pop())

     alert(a.contents)

-Manan

SasiKumarVVS
Inspiring
February 12, 2019

Thanks Mr.Manan. That works fine.

Regards,

Sasi

spicyDoge
Known Participant
February 11, 2019

if youre wanting to consolidate those empty lines, then do the following

  1. doc.findGrepPreferences.findWhat = "[\v]+"; 
  2. doc.findGrepPreferences.changeTo = "\r";
  3. doc.findGrep();

then you can string.slice it using \r, or \r\r if you want to skip the prev step

Community Expert
February 12, 2019

Manan Joshi​, Spicy dog -- Y'all forgot to escape your backslashes: "\\n|\\r" not "\n|\r" and "[\\v]+" not "[\v]+"

By the way, classes are more efficient than alternatives: [\n\r] works better than \n|\r

And there's no need for single-item classes: \v+ works just as well as [\v]+ (don't know if there is any difference in efficiency/speed).

P.

Community Expert
February 12, 2019

Ahh, Peter Kahrel that's where accustomed and experienced eyes come into picture. I just coded the idea without testing(picked it up from the Find/Change UI), i know this is never an excuse especially when its being told to someone in need of help. I hope i won't forget it henceforth, and regarding the efficiency thing that you mentioned, i will keep a note of that as well. Thanks for sharing your knowledge.

-Manan

Community Expert
February 11, 2019

Use the following code to find the soft return and hard return i.e. paragraph break character. Then you can replace these characters with whatever you want to do, i am not all clear on what you need to do.

app.findGrepPreferences.findWhat = "\n|\r";     

var match = app.activeDocument.findGrep(); 

// Clear the find/change preferences after the search 

app.findGrepPreferences = NothingEnum.NOTHING;

The match would be an array of characters that are either \n or \r in the whole document.

-Manan