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

Find the + symbol using Grep

Engaged ,
Sep 15, 2017 Sep 15, 2017

Hi All,

I need find the " + " symbol between two ampersand symbols. I will tried the grep code and find the all text in my Doc.

used grep code: \&.+?\&

Example: [&~NOSTYLE~ + {~NOSTYLE~[TimesTen Italic%P%100.0]+~rom~~norm+~&]

How to find the plus symbol inside the ampersand symbol(&) ?

Thanks,

Prabu G

Thanks,
Prabu
Design smarter, faster, and bolder with InDesign scripting.
TOPICS
Scripting
2.6K
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 , Sep 16, 2017 Sep 16, 2017

In that case you need to find all instances of the code fragments, then replace + with |+|. Something like this:

app.findGrepPreferences = null;

app.findGrepPreferences.findWhat = '\\[&.+?&\\]';

found = app.documents[0].findGrep();

for (i = found.length-1; i >= 0; i--) {

  found.contents = found.contents.replace (/\+/g, '|+|');

}

Untried, but you get the picture.

P.

Translate
Community Expert ,
Sep 16, 2017 Sep 16, 2017

You should escape the +, no need to escape the ampersands: &\+& will do.

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
LEGEND ,
Sep 16, 2017 Sep 16, 2017

Hi Prabu,

Very simply (including the opening/closing brackets):

(\[&|\G).*?\K\+(?=.*&\])

(^/)  [ advanced Grep code -- To be validated! ]

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
Engaged ,
Sep 16, 2017 Sep 16, 2017

Hi Peter and Obi,

Thanks for your reply.

sorry for the above....

Am working with ASCII code for powerMath equation. so i need to find inside the ASCII codes([&......&])  "+"(only)  symbol and change to "|+|".

Thanks,

Prabu G

Thanks,
Prabu
Design smarter, faster, and bolder with InDesign scripting.
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 ,
Sep 16, 2017 Sep 16, 2017

In that case you need to find all instances of the code fragments, then replace + with |+|. Something like this:

app.findGrepPreferences = null;

app.findGrepPreferences.findWhat = '\\[&.+?&\\]';

found = app.documents[0].findGrep();

for (i = found.length-1; i >= 0; i--) {

  found.contents = found.contents.replace (/\+/g, '|+|');

}

Untried, but you get the picture.

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
Engaged ,
Sep 16, 2017 Sep 16, 2017

Hi Peter,

Amazing. Simply amazing. Works like a charm now in every instance.

Thanks

prabu G

Thanks,
Prabu
Design smarter, faster, and bolder with InDesign scripting.
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
LEGEND ,
Sep 16, 2017 Sep 16, 2017

Prabu,

Do you mean mine not?

app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.findWhat = "(\\[&|\\G).*?\\K\\+(?=.*&\\])";

app.changeGrepPreferences.changeTo = "|+|";

app.activeDocument.changeGrep();

(^/) 

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 ,
Sep 16, 2017 Sep 16, 2017

Very nice, Michel. Didn't know about \G

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
LEGEND ,
Sep 16, 2017 Sep 16, 2017

Only some Jedis know it! [it comes from the Dark Side]

I gave it a name: the \Ghost! 

(^/)

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
Guide ,
Sep 17, 2017 Sep 17, 2017

Hi Prabu,

Keep in mind there are still possible issues in using (\[&|\G).*?\K\+(?=.*&\]) as it doesn't fully deal with extra + signs.

For example in the text:

X+Y+[&A+B+C+D&]+X+Y[&A+B&]+X+&]+[&X¶

+A&]+B[&X+Y&]+[&X+Y++Z&]++C+D&]¶

we may expect GREP to alter only the well-formed red areas. But it won't be the case with uncontrolled .* in your regex.

So, in your find/change script you might need to refine the suggested syntax,

const GREP1 = "(\\[&|\\G).*?\\K\\+(?=.*&\\])";

// etc.

into something more explicit like

const GREP2 = "(\\[&|\\G)[^&+\\r]+\\K\\++(?=[^&\\r]+&\\])";

// etc.

Given the same input, the figure below shows how the + signs are captured and changed (see the red crosses ×) using GREP1 vs. GREP2. You can see that GREP1 eats a number of + signs which are not embedded in the [&…&] regions, while GREP2 is a bit more accurate:

grep.png

But of course there is no definitive answer as long as we don't have more specification on the actual characters your stream may contain inside and outside the markup.

@+

Marc

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 ,
Sep 17, 2017 Sep 17, 2017
LATEST

Marc -- When you use the single-line modifier (?s) the dot matches the return character, which gets around the problem you noted that the original grep expression doesn't deal with internal line breaks.

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