0

I have the following string:

Character (ccdd)

The c's and d's are necessary for a stupid design reason. I want to highlight c's and d's specifically, so that

a) matches "cc" and b) matches "dd"

with the requirement that it only matches if it is in parentheses.

But I just don't get to it. The only thing I managed so far is:

(?<=\()[c]+?(?=\))

Edit: To further clarify. I need to apply two separate styles to c's and d's in parentheses in InDesign. Therefore I am looking for two separate regex expressions, one that matches all c's in the parentheses and one that matches all d's.

Anyone an idea?

Thank you!

kftb
  • 117
  • 2
  • 11
  • can you confirm one thing:- check where `\G` works in indesign? – rock321987 May 26 '16 at 15:18
  • [`(?<=\([^()]*)c+(?=[^()]*\))`](http://regexstorm.net/tester?p=(%3f%3c%3d%5c(%5b%5e()%5d*)c%2b(%3f%3d%5b%5e()%5d*%5c))&i=Character+(ccdd)) - if infinite width lookarounds are supported. – Wiktor Stribiżew May 26 '16 at 15:19
  • I don't quite understand what you want to do. Is the string you want to match always `(ccdd)`? Then you can use that as a literal pattern. – Håken Lid May 26 '16 at 15:21
  • if `\G` is supported:- **[`(?:\(|\G(?!\A)).*?((\w)\2)`](https://regex101.com/r/jZ8zG0/1)** – rock321987 May 26 '16 at 15:23
  • You guys are quick! @rock321987 doesn't show any matches in InDesign, neither does Wiktor Stribiżew solution. \G is not supported unfortunately, as far as I can see. I should have been more precise: I am looking for two regex expressions, one that matches all c's in the parentheses and one that matches all d's in the parentheses (so basically one but just for each character) – kftb May 26 '16 at 15:35
  • 1
    Try a hack `c+(?=[^()]*\))` and `d+(?=[^()]*\))` – Wiktor Stribiżew May 26 '16 at 15:50
  • 1
    Works beautifully, @WiktorStribiżew. Thank you so much, and to all the others. Do you wanna post it as answer for others to see? – kftb May 26 '16 at 16:00
  • I aded the comment as an answer. – Wiktor Stribiżew May 26 '16 at 16:14

1 Answers1

2

Since the infinite lookbehind and \G are not supported, I suggest using a hack: just checking if the cs or ds are followed with a closing parenthesis.

Use

c+(?=[^()]*\))

and 

d+(?=[^()]*\))

That hack will work if the parentheses are well balanced and if there are no nested parentheses.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • The latest versions of InDesign support `\K`. Can that be used as an alternative for your `\G` solution? (I don't know what `\G` does but it sounds pretty much the same.) – Jongware May 29 '16 at 12:57
  • `\K` omits all the text in a match buffer. If `\K` is supported, `\G` (matches the beginning of a string or the end of the previous match). It could be matched with some pattern like [`(?:\(|(?!^)\G)[^()]*?\Kc+(?=[^()]*\))`](https://regex101.com/r/lI9hW9/1). – Wiktor Stribiżew May 29 '16 at 13:00