1

I try to apply a style to a string of words that is beginning a paragraph and before a colon.

I prepared a first Grep search but it is not working (it is only placing the cursor before the colon without selecting anything...

(?=^)*?(?=\:)

Any idea?

2

In the beginning you have (?=^). A Positive Lookahead that looks ahead to see if a Beginning of Paragraph comes after whatever text you have before the lookahead (in this case none). Then you have *? which searches for Zero or More Times (Shortest Match) of that Positive Lookahead. It's a bit malformed. You don't actually select any characters.

For your approach to work you would have to use a Positive Lookbehind (?<=^) that looks behind to see if a Beginning of Paragraph comes before the text without selecting it. Then use .*? to select any character Zero or More Times (Shortest Match) and a Positive Lookahead (?=:) to end the selection before a colon:

(?<=^).*?(?=:)

But you can write it simpler. ^ is always non-matching, so you don't need the lookbehind:

^.*?(?=:)

8
  • ^ itself is non-matching, so it’s not necessary to do a lookbehind at all – in fact, I wouldn’t have thought a lookbehind for a ^ would even work at all. Your version will also match non-initial strings, though (i.e., if there are two colons, it will match the string between them), so a ^ is needed. *? is perfectly valid: it’s a non-greedy wildcard match. You could even use this to make the query a bit simpler: ^.*?: will match the same as ^[^:]*: (including the colon), and ^.*?(?=:) will match the same as your ^[^:]*(?=:) (with the extra ^). Feb 1 '21 at 20:23
  • 1
    I’m guessing the reason why the original query just places the cursor before the colon is that (?=^) by definition cannot match anything, but is apparently not invalid syntactically, so it’s equivalent to an empty, non-matching group. This empty group (= anything that’s not a non-empty match, i.e., any position between characters) is then matched non-greedily up until a position where it’s followed by a colon (from the lookahead) – that is, the position between the colon and the character immediately preceding it. Definitely not normal use of GREP, though! Feb 1 '21 at 20:28
  • @JanusBahsJacquet, using the non-greedy wildcard *? is nice, but I don't understand why you feel the need to have ^ in the beginning. To me it seems that .*?(?=:) works just as well as ^.*?(?=:). See here and notice how it doesn't select the string between the colons. Am I missing something?
    – Wolff
    Feb 1 '21 at 21:15
  • @JanusBahsJacquet, and please, if I say something wrong about GREP in my answer, don't hesitate to fix it!
    – Wolff
    Feb 1 '21 at 21:19
  • That’s in a GREP style, which only matches once per paragraph. The question mentions a GREP search, which will match multiple times per paragraph (unless the query contains a paragraph mark or $, of course). If you do a find/replace-all with the same query, it should match the string between the colons as well. Feb 1 '21 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.