Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx for matching forward reference exercise

Tags:

regex

I am working on an exercise on regex forward reference like the one asked here

The task is:

  • string s consists of tic or tac
  • tic should not be immediate neighbour of itself
  • The first tic must occur only when tac has appeared at least twice before

I've used this regex to solve it. But it doesn't use forward reference so I really want to know how this can be solved using it.

^tac(tac)+(tic(tac)+){0,}(tic)?$

Valid:

tactactic
tactactictactic

Invalid:

tactactictactictictac
tactictac

How do I solve this problem?


1 Answers

This uses a forward reference:

^(\2tic|(tac))+$

Demo

It's actually very similar to the example given on this page.

like image 55
sp00m Avatar answered Jun 08 '26 16:06

sp00m



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!