1

I'm writing a syntax highlighter for the Sublime Text editor (that uses the Oniguruma regex definitions). Basically, I have something like this:

Regex: \((\w+\s*)*\)

Test: (how are you)

Capturing groups: 1. you

My problem is, that only the last occurrence in the capturing group is matched (and therefore highlighted) and not the entire content of the capturing group.

In my concrete case:

Regex: \(\w+(\s+(\?\w+\s+)+-\s+(\w+))*\)

Test: (at ?l - location ?x - object)

Capturing groups:

1. ?x - object
2. ?x
3. object

However, I'd like to match the entire group content, like that:

1. ?x - object AND ?l - location
2. ?x AND ?l
3. object AND location
MattDMo
  • 100,794
  • 21
  • 241
  • 231
Pold
  • 337
  • 1
  • 11
  • I'm not sure I understand. A single capturing group is only going to have one value assigned to it for a given match. When you write "AND" in the group matches, are you saying that you want these two values to be simultaneously held in \1, \2, and \3, or do you just want the literal values to be "?x - object ?l - location", "?x ?1", and "object location"? – CAustin Mar 05 '14 at 17:36
  • Yes, I'd like to have two (or more) values to be simultaneously held in one capturing group (but apparently this is not possible). My problem is, that only `?x` and `object` get highlighted in my example, but I'd like to assign one color to `?x` and `?l` and another color to `object` and `location` (and accordingly in my first example, I'd like to highlight `how`, `are` and `you` and not just `you`). – Pold Mar 05 '14 at 17:54

1 Answers1

0

The regex ensure finding of all occurrences not just the last one.

(?:\(at |(?!^)\G)(?:(?=(?:(?!\(at ).)*\))[^?\w])*?(?:(?<token1>\?\w+)|(?<token2>\w+))

Demo

http://regex101.com/r/rF8pM2

From your example, your will ?l and ?x in a named capture group called token1. object and location can be accessed with the named capture group token2.

Stephan
  • 41,764
  • 65
  • 238
  • 329