Skip navigation
Currently Being Moderated

Using GREP to style text - help!

Sep 20, 2012 9:22 AM

Tags: #grep

I'm trying to learn GREP to style text... and banging my head off a brick wall!

 

The text is:

Subject | Institution | Date

Lorem ipsum dolor...

 

I want to format the text before the first vertical line (|) as bold, the text between the first and second vertical lines as italic, and a third style for the text between the second vertical line and the carriage return. The remaining text (Lorem...) will be the regular paragraph style. Seems pretty straightforward....

 

I'm comfortable with setting up the relevant character styles for the formatting I require, I just can't work out how to select the relevant text. I can't even figure out how to express 'select text before' a particular caharcter like '|'!

 

Can anyone help me write the GREP expression, or point me in the direction of some examples that contain similar expressions? I've found some cheatsheets online (below), but none contain the 'select text before' expression.

 

http://www.night-ray.com/regex.pdf

http://www.ericagamet.com/wp-content/uploads/2011/11/Erica-Gamets-GREP -Cheat-Sheet.pdf

 
Replies
  • Currently Being Moderated
    Sep 20, 2012 9:40 AM   in reply to leepaulvickers

    Honestly, if Subject | Institution | Date is always the beginning of a paragraph, I'd do this  with nested styles, not with GREP:

     

    Untitled.png

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 20, 2012 2:34 PM   in reply to leepaulvickers

    Oh, but with grep... it's more fun! And what if you don't want to touch (format) those pipe chars?

    I think you should look closer to "look behind" and "look ahead" expressions.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 20, 2012 2:37 PM   in reply to winterm

    Ach. Yeah, the nested style should have been "up to" not "through." Then the character style would not be applied to the pipe characters. I tried to gin up a GREP query, but couldn't pull it off; it's beyond me. Besides, since more than one character style needs to be applied, it ought to take more than a single query to apply both bold and italic styles. That's unnecessary duplication of effort, if you ask me.

     

    Besides... are you going to take a stab at it?

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 20, 2012 2:53 PM   in reply to leepaulvickers

    I would also use nested styles, like Joel suggested, but this will escape pipe character:

     

    Style 1 up to 1 |

    [None] through 1 Character

    Style 2 up to 1 |

    [None] through 1 Character

    Style 3 up to 1 \r

     

    Hope that helps.

     

    --

    Marijan (tomaxxi)

    http://tomaxxi.com

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 20, 2012 2:54 PM   in reply to Joel Cherney

    huh, I have a project every month, where specific line of digits and some chars is used hundred and hundred times...with  very different formatting - up to 8 (yes, eight!) different char styles. All is build on grep, all I need to do - just click para style. I'd be dead to do it manually.

    So its surely doable, and I guess OP wants just the direction. I think it's "look ahead" and "look behind".

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 20, 2012 3:37 PM   in reply to winterm

    As Joel said, to apply three character styles you'd need three GREP styles, though they could all be in the same paragraph style. Nested styles, however, if they will work with the constuction of the paragraph, are FAR more efficient for processing.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 20, 2012 3:37 PM   in reply to winterm

    All is build on grep, all I need to do - just click para style.

     

    Aha! It's not a GREP query, but a collection of GREP styles. And you didn't write 'em! Busted!

     

    Yeah, assuming that there are only two pipes in any one paragraph, and that there are never paragraphs with pipes that should not have these styles applied, then it would be possible to write GREP queries that looked for these patterns. Okay, here's the query that finds the middle one between the pipes:

     

    (?<=\|).+(?=\|)

    That's a positive lookbehind for a pipe, then "just find anything, anything at all," then a positive lookahead for a pipe. If you made a GREP style in your paragraph style with this query, and used it to apply an italic character style, then you... er, I, I mean I... would have spent about four times as much effort on a GREP style than I would have on half of a nested style.

     

    Whatever floats yer boat, wintermute.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 20, 2012 3:56 PM   in reply to Joel Cherney

    Joel Cherney wrote:

     

    All is build on grep, all I need to do - just click para style.

     

    Aha! It's not a GREP query, but a collection of GREP styles. And you didn't write 'em! Busted!

     

    no, I wrote them... but yes, that is a collection of styles. Somewhat like this:

    grep.png

     

    really not a big piece of art, but it works, and can be used as a starting point.its up to everyone to decide, what to use. What and when. Frankly, I almost forgot nested styles even exist...

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 20, 2012 4:01 PM   in reply to winterm

    That's clever, using a positive lookahead with the beginning-of-paragraph marker. I (obviously) didn't think of that. Okay, I retract my callout.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 21, 2012 6:53 AM   in reply to leepaulvickers

    Let me repeat what I said earlier about GREP Styles and processing efficiency. GREP Styles check the text at every keystroke to see if there pattern is matched. A number of experienced regulars here have reported significant slowdowns in performance when GREP Styles are used heavily in their files. I firmly believe using a nested style instead, whenever appropriate, is a better workflow.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 22, 2012 3:12 AM   in reply to Peter Spier

    Huh, Peter,

    with all my respect - grep styles are much more powerful and flexible than nested styles, you can do A LOT more things with it. Maybe its overkill to use grep in this particular case, however, let's treat it as a part of "learning grep" process for OP. Excuse me for reminding, he asked for "grep solution" (not nested styles), and here it is.

    Now your answer seems to me like:

    "Q.: How can I do it in InDesign?

    A.: You shouldn't do it in InDesign, you better do it in Word, because in this particular case it's more efficient"

     

    Huh?

    I see efficiency as a secondary thing this time.

     

    @leepaulvickers:

    among other good sources, I believe you should give a try to this nice thingy, called What the GREP:

    http://www.jongware.com/idgrephelp.html

     

    ah, talking about scripts and greps... you better read twice everything, signed with this name... if you can't dig in it today, bookmark it and come back tomorrow.

    Jongware is The Master.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 22, 2012 6:14 AM   in reply to winterm

    @ Winterm,

     

    GREP styles are absolutely more powerful and flexible than nested styles, and they certainly are an important part of the toolkit, but you don't use a sledgehammer to drive tacks.

     

    If my memory serves, both Dave Sanders and Jongware are among the users who have reported ID lagging when more than a few GREP styles are in use, and that the same files suddenly come to life when the GREP styles are removed. This may not be a problem for everyone, obviously, but if you use a lot of GREP styles and base other paragraph styles on paragraph styles that use GREP styles, you need to be careful or you could find yourself waiting for ID to respond while you type.

     

    It's kind of like taking the the 200 lbs of wintertime sand out of your car in the summer to boost your fuel economy if you live in an area with lots of snow.

     

    As for your analysis that I'm saying don't do it in ID because it's inefficient, and that we should supply the GREP solution because that's waaht was requested, well, nested styles are a part of ID, too, and one that the OP also seems to need to learn. I'd compare this more to a case of teaching someone to use styles instead of telling them how to use the eyedropper to pick up local formatting and apply it hundreds of times through a document.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 22, 2012 8:56 AM   in reply to Peter Spier

    Well, let's see. The original poster expressly wanted to learn how to write a GREP expression. The OP learned how to write that expression, and also learned one way of employing such an expression - in a GREP style.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 22, 2012 9:01 AM   in reply to Joel Cherney

    You left out that he also learned that for this particular patter a nested style is better (at least inthe opinion of several of us who responded).

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 22, 2012 9:05 AM   in reply to Joel Cherney

    But a GREP style, while incredibly useful in many circumstances, is not the most efficent tool for the job. I too have experienced some significant lag when my document is overladen with GREP styles. I've not seen that with nested styles. The fact that GREP is more fun is undeniable but when you say "I forgot that nested styles even exist" it means that you're over-using your Swiss Army knife and need to take a good look at your Torx wrenches.

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 22, 2012 11:32 AM   in reply to Peter Spier

    Peter Spier wrote:

     

    [...] you don't use a sledgehammer to drive tacks.

    hm, sometimes I use... and see no devil in it if sledgehammer remains under control

     

    [...] ID lagging when more than a few GREP styles are in use [...] you could find yourself waiting for ID to respond while you type.

    In one of my every-month project I use A LOT of grep styles with no problems. Even more, I'm sure they are miles away from perfect - I wrote them myself about year or two ago...

    And... sorry, I'm a designer, not a typist. There is some word processing proggies for typing

     

     

    nested styles are a part of ID, too, and one that the OP also seems to need to learn.

    absolutely agreed! So far so good for OP - he learned both ways, that's the right method of learning.

     

     

    I'd compare this more to a case of teaching someone to use styles instead of telling them how to use the eyedropper to pick up local formatting and apply it hundreds of times through a document.

    sorry, couldn't agree again. In my above mentioned project I use eyedropper so heavily, I even created kb shortcut for it while being in text mode - just for copying text attributes. Styles not always serves you right way, so do nested or grep styles... Everything depends on specific project workflow and, well, your own "style" and preferences.

     

    Simply: you must know as much as possible ways to achieve your goal and... choose wisely every moment.

     

    @Joel:

    thank you for understanding my formal ground

     
    |
    Mark as:
  • Currently Being Moderated
    Sep 22, 2012 11:24 AM   in reply to Joel Cherney

    Joel Cherney wrote:

     

    But a GREP style, while incredibly useful in many circumstances, is not the most efficent tool for the job

    "Most efficient tool" is your brain. Always.

    yes, maybe i'm overusing grep, but please don't kick me, it's still a new interesting toy for me... I still have a lot to learn about it, how could I without using it?

     

    [...] when you say "I forgot that nested styles even exist" it means that you're over-using your Swiss Army knife and need to take a good look at your Torx wrenches.

    Yes, and I'm thankful to You for reminding. I'll definitely take another look to it. After few years of oblivion...

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points