I need a regex to match tags that looks like <A>, <BB>, <CCC>, but not <ABC>, <aaa>, <>. so the tag must consist of the same uppercase letter, repeated. I’ve tried <[A-Z]+>, but that doesn’t work. of course I can write something like <(A+|B+|C+|...)> and so on, but I wonder if there’s a more elegant solution.
I need a regex to match tags that looks like <A> , <BB> ,
Share
You can use something like this (see this on rubular.com):
This uses capturing group and backreference. Basically:
(pattern)to “capture” a match\nin your pattern, wherenis the group number, to “refer back” to what that group matchedSo in this case:
([A-Z]), an uppercase letter immediately following<\1*, i.e. zero or more of that same letterReferences