0

I have a bunch of variable text in Adobe Indesign and I'd like the first mentioned price in each line of text to have another color. I am completely new to regular expressions and got stuck. I used the following grep to find the price but it find's all prices mentioned in the text.

€[0-9]+([.,][0-9]+)*

The text is like below and I need to get only the bold prices:

Spend €99,99, get €25 off

Spend €150, get €35 off

I tried lot's of things mentioned on stack overflow and online but just can't find the right solution. Adding ? at the end to make it lazy doesn't do the trick. I think I need to do some grouping on the grep-code but can't find the right way to do so. Any help would be great!

Mackie
  • 3
  • 1
  • Not sure if it is supported, but you could use a non greedy quantifier and get the value from the first capturing group `^.*?(€[0-9]+(?:[.,][0-9]+)*)` https://regex101.com/r/xUMvYo/1 – The fourth bird Feb 14 '20 at 10:05
  • Thnx 4thBird, it is close but suggested code also selects everything in front of the first price. (Like this: **Spend €99,99**, get €25 off). – Mackie Feb 14 '20 at 10:37
  • It matches everything, but the value you are looking for is in the first capturing group. Perhaps you can refer to it using `\1` or `$1` – The fourth bird Feb 14 '20 at 10:40
  • Instead of doing a whole bunch of searches, you may want to look into **GREP Styles** – cybernetic.nomad Feb 14 '20 at 22:43
  • 4th bird: I don't (yet) know where to add that in the expression. – Mackie Feb 17 '20 at 07:21
  • Everything I tried resulted in an error. But I'm gonna take a course so that won't be an issue in the future. RobC, Cybernetic.nomad: I'm indeed using grep in a style because I'm using the text as a variable in Indesign using XMPie. But if I understand RobC correctly the thing I'm looking for just can't be done. – Mackie Feb 17 '20 at 07:27

2 Answers2

1
  1. create two character styles "Bold" and "Roman", the latter should be the same character style as in your paragraph style.

  2. Create two GREP styles. The first one should apply character style "Bold" to text with the following pattern:

    ^.*?(€\d+(?:,\d+)*)
    

    the second one should apply character style "Roman" to text with the following pattern:

    ^.*?(?=€)
    

enter image description here

cybernetic.nomad
  • 6,100
  • 3
  • 18
  • 31
  • Thanks nomad, for some reason it won't work here (I did notice the missing bracket in the first pattern so that ain't it - in your screenshot is the good one by the way). When I add the first pattern it selects everything before and including the first value. When adding the second one it selects everything before and including the first value ánd the first word after the value. When I have more time I'll start a new document and try it again. – Mackie Feb 20 '20 at 15:06
  • @Mackie: you must have forgotten to adjust the character styles. The behavior of the first style is full "by design"; the second style is to override the bold text again with a Roman (or Regular, or Medium or whatever-you-got) style. – Jongware Feb 24 '20 at 14:12
  • 1
    @usr2564301 I believe the typo is now fixed, thanks for spotting it – cybernetic.nomad Feb 24 '20 at 15:11
  • Only out of curiosity: did you also experiment with `^[^€]*\K...` where the `\K` is supposed to reset the match? – Jongware Feb 24 '20 at 15:18
  • Thanks cybernetic.nomad and usr2564301. I created a new document and it is what I was looking for. Works just great. Thanks! – Mackie Feb 28 '20 at 08:07
1

You can use the code \K to reset matching from the start. This works:

^[^€]*\K€\d+([.,]\d+)*

because it first matches everything up to the first occurrence of , then forgets what it just matched, and after that only matches the Euro sign and the next amount.

GREP style in InDesign CC 2020

Jongware
  • 22,200
  • 8
  • 54
  • 100
  • That works but it won't find the first prices in the following paragraphs anymore. Thanks for the answer though. Learned a lot from this case. – Mackie Feb 28 '20 at 08:09
  • 1
    @Mackie: then your paragraphs are not separated by hard returns, but you are using Shift-Returns instead. Don't do that, for various good reasons (this is one). – Jongware Feb 28 '20 at 09:16
  • I checked it and you are right. Problem with this is that we are using personalization-software and the text is imported (and outputted) directly from an excel-worksheet wherein the customer has used these shift-/soft-returns (alt-return in excel) instead of hard-returns. I guess hard-returns aren't even possible in excel as a hard-return is the end of a record/line. – Mackie Mar 02 '20 at 07:18