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

Use GREP to find \r or \n

Explorer ,
Feb 11, 2019 Feb 11, 2019

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

TOPICS
Scripting
2.5K
Translate
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 , Feb 12, 2019 Feb 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.findGrepPref

...
Translate
Community Expert ,
Feb 11, 2019 Feb 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

Translate
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
Explorer ,
Feb 11, 2019 Feb 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

Translate
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 12, 2019 Feb 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.

Translate
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 12, 2019 Feb 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

Translate
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
Explorer ,
Feb 12, 2019 Feb 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.

Screen Shot 2019-02-12 at 11.50.32 AM.pngScreen Shot 2019-02-12 at 11.57.22 AM.png

Regards,

Sasi

Translate
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 12, 2019 Feb 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

Translate
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
Explorer ,
Feb 12, 2019 Feb 12, 2019

Thanks Mr.Manan. That works fine.

Regards,

Sasi

Translate
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
Explorer ,
Feb 14, 2019 Feb 14, 2019

Mr.Manan Could you help me to find the above one?

Regards,

Sasi

Translate
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 14, 2019 Feb 14, 2019
LATEST

Try the following

\[Small\]\n([^0-9]*\n)*\d+\n[^0-9\r]*\r

You have mentioned that all of this is part of a paragraph, i am not sure if you meant it but the regex i gave makes this assumption and each line is seperated by a soft enter i.e. shift enter with only the last line terminating in a hard enter. See the screenshot of my sample document

Screen Shot 2019-02-14 at 4.15.45 pm.png

-Manan

Translate
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
Explorer ,
Feb 13, 2019 Feb 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)

BenzLogo.png

Capture.PNG

Regards,

Sasi

Translate
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
Explorer ,
Feb 13, 2019 Feb 13, 2019

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

find

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

replace

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

Translate
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