2

Using regular expressions, is it possible to match a number of characters corresponding to the length of a previously captured group ?

I would like to match the following :

SomeLengthyPattern :
some_stuff         :
some_other_stuff   :

My idea was capturing SomeLengthyPattern and then using its length as a base to match some_stuff and some_other_stuff.

zx81
  • 38,175
  • 8
  • 76
  • 97
DaLynX
  • 171
  • 9

1 Answers1

1

Surprisingly... Yes!

With a handful or regex engines, you can make sure that one token is matched the same number of times as a previous token.

  • In .NET, you can do it with balancing groups.
  • In PCRE and a few other, you can use what is known on this site as the Qtax trick. The requirement is that a capture Group must be able to contain a back-reference refer to itself.

Warning! These techniques are advanced. If you proceed further, your regex skills will improve tremendously.

Resources

Community
  • 1
  • 1
zx81
  • 38,175
  • 8
  • 76
  • 97