1

In InDesign's GREP search I'm trying to get parts of a text field which was filled with a CSV (file using # as delimiter). The reason I want to grep the different parts is to give each a different character style.

The Textfield content looks like this:

Alpha#60x50cm#Acryl
Beta#2013#50x40cm#Öl
Gamma#2013#50x40cm#Holz
…

Using

^[^#]+

would work fine to get the first part of each line, (which is delimited by the first hashtag)

enter image description here

How would Grep pattern look like to get:

  • everything between hashtag 1 and 2,
  • everything between hashtag 2 and 3,
  • everything after hashtag 3?

In every line.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Anatol
  • 1,923
  • 6
  • 26
  • 55
  • Can you say what must happen when you *find* these items? If this is for while working in the UI, GREP is your best shot. However, if this is to work inside a script, there are other -- easier! -- options than a complicated GREP. – Jongware Jan 18 '15 at 14:09
  • Which grep you mean? try this `(?<=#)[^#\n]*(?=#|$)` regex. – Avinash Raj Jan 18 '15 at 14:20
  • Hi Jongware, I want to assign different character style to each part. With the grep I posted I get every character until the first hashtag (in every line) with this I could attach f.e. a character style called "headline". Next I want format every character between first and secong hashtag with cahracter style "subtitle". that´s why I need the greps – Anatol Jan 18 '15 at 14:32
  • @tBook: to make sure an earlier entrant in the discussion thread notices your comment, you can "ping" them with an `@` character before his/her name. Using `@jongware` will indicate in the top bar that I have a message. The SO interface helps with auto-completing names from the current discussion. – Jongware Jan 18 '15 at 22:29
  • @Jongware thanks did not knew that. – Anatol Jan 19 '15 at 07:58
  • It's still basically the same question. I tested my answer and it does what you seem to be asking, doesn't it? – Jongware Jan 19 '15 at 09:34
  • @Jongware Sorry it seems I did not recognize your answer when commenting. It works perfectly. -Thank you very much- The JS solution is a good hint as I code it, seems I have to dive more into Indesign Scripting ;) – Anatol Jan 19 '15 at 12:42
  • As you can see it also works nicely using GREP styles. The interface between ID's own formatted text and Javascript is ... clunky. It *can* be very efficient, but it requires quite a firm grasp of JS. Should you try and get to a point where you *almost* got it working, don't hesitate to post another question. – Jongware Jan 19 '15 at 12:48

2 Answers2

2

In the InDesign UI, you can

  1. search for ^[^#]+ to match the contents of the first field;
  2. search for (?<=#)[^#]+(?=#[^#]*#[^#]*$) to match the contents of the second field;
  3. search for (?<=#)[^#]+(?=#[^#]+$) to match the contents of the third field;
  4. finally, search for (?<=#)[^#]+$ to match the contents of the fourth field.

This relies on all lines having exactly four fields (and thus 3 #), and will match only the text in between two # markers. Here is an image showing the result with these four applied as GREP styles:

grep styles to highlite fields

As you can see, the applied attributes overlap nowhere, so everything is only marked once.

Another way is to use Javascript. All of the lines can be split on the # character (the command is called split), and that returns an array of the text in between. However, this converts the text into a plain Javascript string, and it takes some trickery to translate the result "back" to native formatted text.

Jongware
  • 22,200
  • 8
  • 54
  • 100
1

You need to use -P parameter with grep.

$ grep -oP '(?<=#)[^#]*(?=#|$)' file
,2013
60x50cm
Acryl
2013
50x40cm
Öl
2013
50x40cm
Holz

To change the above command's output as three columns per line.

$ grep -oP '(?<=#)[^#]*(?=#|$)' file | paste - - -
,2013   60x50cm Acryl
2013    50x40cm Öl
2013    50x40cm Holz
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274