27 Replies Latest reply on Jun 4, 2014 3:22 PM by Peter Spier

    Grep style targeting text between commas

    vazrick2

      Hello InDesigners!

       

      I have a list of names in a single paragraph each separated by a comma, like this...

       

      John Doe, Jane Smith, Sally Sell

       

      I'd like to target each first and last name with a No Break character style nested in a paragraph style.

      I found this useful "find between" article and grep code here: 

       

      (?<=\().*?(?=\))

      http://indesignsecrets.com/findbetween-a-useful-grep-string.php

       

      But InDesign / Grep doesn't seem to have a symbol for spaces and I'm not sure how to escape out the space or how exactly I'd need to work with the ", " (comma and space).

       

      Any ideas?

       

      Thanks!

      Rick

        • 1. Re: Grep style targeting text between commas
          Willi Adelberger Most Valuable Participant

          Maybe that it would be easier to do it the other way round: Make the whole paragraph no break, but apply the comma and the following space to break, this could be performed either with GREP or with nested styles (repeat).

          • 2. Re: Grep style targeting text between commas
            Willi Adelberger Most Valuable Participant

            Of course, you can target a space:

            • Type a simple space for a normal space
            • \s —Any Space
            • ~S —Protected Space
            • ~s —Protected Space with fixed width
            • ~m —M-space
            • ~> —N-space
            • ~f —Flushing Space
            • ~< — 1/8 of an M-Space
            • ~| —1/24th of an M-Space
            • 3. Re: Grep style targeting text between commas
              vazrick2 Level 1

              Thanks Willi, but maybe my post wasn't clear.

               

              I'm wanting to turn this paragraph below with column width specified...

               

              |--------------------------------------------------------------------|

              John Doe, Jane Smith, Sally Sell, James

              Z. Spiff, Spinal T. Twist, John Doe Miller, Sally

              S. Seashells

               

              In to this...

              |--------------------------------------------------------------------|

              John Doe, Jane Smith, Sally Sell,

              James Z. Spiff, Spinal T. Twist, John Doe Miller,

              Sally Sells Seashells


              The idea is that every line in the paragraph should end with a comma and the list would continue. If I changed the width of the column, say in the example below, then James Z. Spiff would appear on the first line and the rest would re-flow accordingly. This would require targeting text that is in between the commas in some way.


              Same example with wider column...

              |---------------------------------------------------------------------------|

              John Doe, Jane Smith, Sally Sell, James Z. Spiff,

              Spinal T. Twist, John Doe Miller, Sally S. Seashells


              Same example too narrow a column to show effect I'm looking for...

              |--------------------------------------|

              John Doe, Jane Smith,

              Sally Sell, James Z. Spiff,

              Spinal T. Twist,

              John Doe Miller,

              Sally S. Seashells


              This Grep here ...

                   (?<=\().*?(?=\))       http://indesignsecrets.com/findbetween-a-useful-grep-string.php

              ...targets everything in between parenthesis.

               

              What I think I need to do is target everything between commas. Or maybe just everything that is preceded by a (comma and space) and followed by a (comma).

               

              Does that help?

               

              Rick

              • 4. Re: Grep style targeting text between commas
                Willi Adelberger Most Valuable Participant

                To escape any character in GREP you can use any Unicode like \x{nnnn}

                nnnn represents the Unicode-Number, which is available in many tables in the internet.

                For the comma the Unicode is 002C, so the escape for the comma would be \x{002C}

                 

                Your solution could also be:

                1. Apply the paragraph style with "No Break"
                2. Create a Character Style where no Break is completely deactivated.
                3. In the Paragraph Style use a GREP like \x{002C}\s to allow any comma followed by any space to break
                • 5. Re: Grep style targeting text between commas
                  Willi Adelberger Most Valuable Participant

                  Try my other response, it will exactly do that.

                  • 6. Re: Grep style targeting text between commas
                    vazrick2 Level 1

                    Kinda there, but I'm not knowledgeable enough about Grep to apply what you've added.

                     

                    here's what I did...

                     

                    Took the original Grep from this:  (?<=\().*?(?=\)) 

                     

                    And replaced the open parens in bold above with your code...  \x{002C}\s

                    And also added part of your code ...   \x{002C}  ... to replace the close parens which is also escaped out.  My resulting Grep snippet looks like this:

                     

                    (?<=\x{002C}\s).*?(?=\x{002C}) 

                     

                    But this did not work. Sorry, my brain's not quite getting it. I want to target text that is in between the comma+space and the comma and force the No Break character style to that text.

                    • 7. Re: Grep style targeting text between commas
                      SJRiegel Adobe Community Professional & MVP

                      I think Will has the fastest, easiest answer. Apply the "no break" style to the whole paragraph (which will cause it to temporarily disappear). The within the paragraph, find and change just  ,  (that is  space-comma) to remove the no break attribute.

                      done.

                      • 8. Re: Grep style targeting text between commas
                        vazrick2 Level 1

                        That did work, thank you!

                         

                        But I'm picky about overrides. This will work for now. I don't like having behavior built in to paragraphs that has to be manually managed with steps where the end-result is mixed overrides throughout the paragraph.

                         

                        But it works!

                        • 9. Re: Grep style targeting text between commas
                          Willi Adelberger Most Valuable Participant

                          I described you, what you need to do. Your GREP is to complicated, you need not look for 2 commas. There are some unknown circumstances yet, because some names can contain dots, some other diacritc signs which you don't include now.

                          STEP0.png

                           

                          1. Set the whole para style to no break. It is ok when the text disappears.

                          Step1.png

                           

                          2. Have a character style prepared which will allow to break:

                          STEP2.png

                          Take care that you will not apply accidentally the character style to the whole frame or future text!

                           

                          3. Now redefine your Paragraph Style in the GREP section. Insert \x{002C}\s to apply the Character Style to a Comma Character and any followed space, you could use in your case also a normal space. But a , followed by a space would do the same, comma and unicode 002C are identical, as you see here. When you click "Preview", you will see how the text becomes visible:

                          Step3.png

                          Even if the columns will be closer, you see the correct reflow of the names:

                          step4.png

                          • 10. Re: Grep style targeting text between commas
                            vazrick2 Level 1

                            Actually, what I did didn't involve Grep per se...

                             

                            1) Selected the paragraph

                            2) Ensured there were no character styles, no "No Break" and no overrides

                            3) Went to the character panel and applied "No Break" manually (paragraph out of view now, still selected)

                            4) CTRL+F to Find/Change dialogue, Text or Grep tab (both work the same)

                            5) In the Find/Change dialogue, I typed in a     comma     and a     space  .... as in what's between the quotes here:  ", "

                            6) In the lower part under Change Format, I added basic character formatting by un-checking No Break so that the box was empty

                            7) Replace All and done!

                             

                            Still though, it's not automatic. Was hoping for something automated that targeted text as part of the paragraph style so there'd be no overrides.

                             

                            This will work for the time being, thanks guys!

                             

                            Rick

                             

                             

                            • 11. Re: Grep style targeting text between commas
                              vazrick2 Level 1

                              Oh!  I get it!

                               

                              Thank you SO much for spelling it out.

                              Nevermind my post above here.

                               

                              :b

                               

                              Rick

                              • 12. Re: Grep style targeting text between commas
                                Willi Adelberger Most Valuable Participant

                                I showed you how it works completely automatic with GREP. What is not working for you. Why are you using Find and Replace. I never recommended it here. Look to my solution it is what you want.

                                • 13. Re: Grep style targeting text between commas
                                  Willi Adelberger Most Valuable Participant

                                  // When I edited the post above the last part dropped out. I added it here. It seems to be a problem here, when someone else adds an answer during the time when someone else is editing something, that the transmition of the data is incomplete.

                                   

                                  3. Now redefine your Paragraph Style in the GREP section. Insert \x{002C}\s to apply the Character Style to a Comma Character and any followed space, you could use in your case also a normal space. But a , followed by a space would do the same, comma and unicode 002C are identical, as you see here. When you click "Preview", you will see how the text becomes visible:

                                  Step3.png

                                  Even if the columns will be closer, you see the correct reflow of the names:

                                  step4.png

                                  • 14. Re: Grep style targeting text between commas
                                    Peter Spier Most Valuable Participant (Moderator)

                                    Since I'm philosophically opposed to apply No Break to text unnecessarily I would go back to applying it only to the spaces. If you don't want the names themselves to break, turn off hyphenation,

                                     

                                    Find (?<=\l)\s(?=\u) which is simply any white space preceded by a lowercase letter and followed by an uppercase. It's not 100% since it would fail with names using things like de or van before the surname, or names with more than two words or an initial, but it will work with the types of names in your example.

                                    • 15. Re: Grep style targeting text between commas
                                      Peter Spier Most Valuable Participant (Moderator)

                                      Actually, it would be even easier to apply no break to spaces not preceded by the comma, if you have only comma separated names: (?<!,)\s

                                      • 16. Re: Grep style targeting text between commas
                                        Willi Adelberger Most Valuable Participant

                                        The Problem here is, that all spaces are no break, except there where a comma is. Even if a dot is between, a dash or other normal breaking symbol, no break is allowed. So here is the normal case "no break" and the exception is the break.

                                        I understand, I would not do it normally, but here it is the other way round as normal.

                                        If you apply no break to anything except comma-space with a no-break-style you are not only between 2 commas, you have to handle the first and the last occurrence differently, because they are not between commas, so I think the negative way is easier here.

                                        • 17. Re: Grep style targeting text between commas
                                          Willi Adelberger Most Valuable Participant

                                          Peter, if you want to use a positve way, I have another solution, which is easy to apply, but not with GREP but with nested styles:

                                          nested.png

                                          1. Now the paragraph style allows to break normally.

                                          2. Now we have a character style which allows no break.

                                          3. The GREP-Section in Paragraph Styles is empty.

                                          4. In the Nested styles I set it up as above in the screen shot:

                                          • Character Style "No Break" up to "," (This does not include the comma!)
                                          • None Style through ", " (Comma and a space character, which is invisible in the screen shot), 2 because each appearance counts, 1 is for the comma, 1 for the following space.
                                          • Now Repeat the first nested styles entries up to the end of the paragraph.

                                           

                                          Nested styles have the advantage that they use less calculation power of your computer and will be faster in long documents than GREP styles which could slow down InDesign, if used in a huge amount. But the result here is the very same as in the other solution I have offered before.

                                          • 18. Re: Grep style targeting text between commas
                                            Peter Spier Most Valuable Participant (Moderator)

                                            As far as I can tell, the only difference in effect between your method and mine is that yours applies No Break to everything except the space after a comma, and mine applies it only to spaces. Yours is likely to result in an overset situation with a long name. Mine will do the same only if hyphenation is disabled.

                                            • 19. Re: Grep style targeting text between commas
                                              Peter Spier Most Valuable Participant (Moderator)

                                              Willi, that's no better. Applying No Break to anything but the spaces is a fundamental error in my opinion.

                                              • 20. Re: Grep style targeting text between commas
                                                pixxxel schubser Level 5

                                                Hello vazrick2,

                                                what is, if you edit your names?

                                                 

                                                I think it's better to use a grep style. There are several possibilities.

                                                Here are two of them:

                                                nr.1

                                                character style with no break (and yellow underlined for showing)

                                                noBreakNames.png

                                                 

                                                nr.2

                                                paragraph style with no break

                                                and character style with break option (and magenta lined for showing)

                                                noBreakNamesVariante2.png

                                                 

                                                Have fun

                                                 

                                                • 21. Re: Grep style targeting text between commas
                                                  Willi Adelberger Most Valuable Participant

                                                  Why? Here are names, and names are NOT allowed to hyphenate in any way. So the no break property has to extend to the whole names. And if a title or prefix is included it must here be in the same line as the name.

                                                  • 22. Re: Grep style targeting text between commas
                                                    Peter Spier Most Valuable Participant (Moderator)

                                                    Willi Adelberger wrote:

                                                     

                                                    Why? Here are names, and names are NOT allowed to hyphenate in any way.

                                                    Says who? Perhaps that's a German convention, but it's guaranteed to cause trouble in a narrow column. You cannot disallow all breaks except between names without going into overset at the first name that is wider than the column.

                                                    • 23. Re: Grep style targeting text between commas
                                                      Peter Spier Most Valuable Participant (Moderator)

                                                      And since this is entirely a list of names, you can prevent them from breaking just by turning off hyphenation, an easily reversible condition.

                                                      • 24. Re: Grep style targeting text between commas
                                                        Willi Adelberger Most Valuable Participant

                                                        But the problem here was to keep together names with all their parts. And if the column is to close it would even be bette to make the break between first and surname than to break any name apart. I think here is not the problem of to close columns as the example was given from the questioner.

                                                         

                                                        If you work in InDesign with names complete correctly, than names would not have applied any language. Otherwise any spell checking procedure will stop by any name. But if no language is applied, no hyphenation is done in InDesign.

                                                        InDesign has a setting to avoid hyphenation with word with capital letters. This is for the English language only useful. Where are capital letters? In headlines, names and beginning of sentences, all cases where hyphenation is not allowed in the opinion of many English speaking people, not only Germans. I was more relaxed in hyphenation than my colleges from England and Australia in my last job.

                                                        In German is this InDesign setting not useful, because we have much more complicated rules for "Großschreibung" than any other Western language, even German scholars have different opinions in the rules of their own language, even in some states (Länder) in Germany are rules different applied, because language is not governed by the federal government. (In Austria it is ruled by the federal government because there is German the official constitutional language, which is required to speak for anyone who wants to get the citizenship or to live there, which is not the case in Germany, there exists neither a constitution nor a constitutional language.)

                                                        • 25. Re: Grep style targeting text between commas
                                                          Peter Spier Most Valuable Participant (Moderator)

                                                          This is a difference in philosophy, and it will not be settled here.

                                                          • 26. Re: Re: Grep style targeting text between commas
                                                            pixxxel schubser Level 5

                                                            Hi Peter Spier,

                                                            I do not think that this a question of a philosophy is. It's only the answer of the question written by vazrick2

                                                            vazrick2 wrote:

                                                             

                                                            … I'm wanting to turn this paragraph below with column width specified...

                                                             

                                                            |--------------------------------------------------------------------|

                                                            John Doe, Jane Smith, Sally Sell, James

                                                            Z. Spiff, Spinal T. Twist, John Doe Miller, Sally

                                                            S. Seashells

                                                             

                                                            In to this...

                                                            |--------------------------------------------------------------------|

                                                            John Doe, Jane Smith, Sally Sell,

                                                            James Z. Spiff, Spinal T. Twist, John Doe Miller,

                                                            Sally Sells Seashells


                                                            The idea is that every line in the paragraph should end with a comma and the list would continue. If I changed the width of the column, say in the example below, then James Z. Spiff would appear on the first line and the rest would re-flow accordingly. This would require targeting text that is in between the commas in some way.


                                                            Same example with wider column...

                                                            |---------------------------------------------------------------------------|

                                                            John Doe, Jane Smith, Sally Sell, James Z. Spiff,

                                                            Spinal T. Twist, John Doe Miller, Sally S. Seashells


                                                            Same example too narrow a column to show effect I'm looking for...

                                                            |--------------------------------------|

                                                            John Doe, Jane Smith,

                                                            Sally Sell, James Z. Spiff,

                                                            Spinal T. Twist,

                                                            John Doe Miller,

                                                            Sally S. Seashells …

                                                             

                                                             

                                                            What I think I need to do is target everything between commas. Or maybe just everything that is preceded by a (comma and space) and followed by a (comma) …

                                                             

                                                            Regards

                                                            • 27. Re: Re: Grep style targeting text between commas
                                                              Peter Spier Most Valuable Participant (Moderator)

                                                              To do that withthe sample text one need only turn off hyphenation and make spaces not preceded by a comma non-breaking, as I indicated earlier. There is no need to apply no break anywhere else if hyphenation is disabled.