techie
techie

Reputation: 363

Regex - Words with 2 different letters repeated twice followed by a number repeated 6 times

I need to regex that matches a string like LLMM222222. I tried with pattern like (\w{2})(\w{2})2{6} but it does not work

Upvotes: 0

Views: 180

Answers (1)

anubhava
anubhava

Reputation: 785846

You can use this regex with 2 back-references:

^([A-Za-z])\1([A-Za-z])\2(\d)\3{5}$

RegEx Demo

RegEx Breakup:

  • ^: Start
  • ([A-Za-z]): Match a letter and capture it in group #1
  • \1: Make sure we repeat same letter using a back-reference #1
  • ([A-Za-z]): Match a letter and capture it in group #2
  • \1: Make sure we repeat same letter using a back-reference #2
  • \d: Match and capture a digit in capture group #3
  • \3{5}: Make sure we repeat same digit 5 more times using a back-reference #3
  • $: End

Upvotes: 4

Related Questions