How do I match a specific sequence of characters only if the sequence has the same amount of characters in another sequence in python regex?

Blanco Yisom :

Is there a way to check to see if each unique character occurs the same amount of times? Soley using the regex language or do I have use regex and str.count to find a valid match?

This is what I'm expecting the results to be:

xy     -> valid
xxyy   -> valid
xxxyyy -> valid
xyy    -> not valid
xxy    -> not valid

I know I can do something like:

matches = re.match(r"^x+y+$", myTestStr)

But is there any way I have the '+' token for 'x' be the same as the '+' token for 'y'? I've also tried working with groups but have had no luck there either.

Predicate :

You could use recursion for this, but to be honest it is not the best practise for the task. Regex are not meant to be recursive. And recursiion, as far as I remember is not insluded in the standart re python module. I think you need the regex module then

if empty should be allowed:

\b(x(?1)y|)\b

if empty string should not be allowed

\b(x(?1)y|(?<=x)(?=y))\b

demo https://regex101.com/r/x0McMc/3

if you have a maximum fixed length of the string you could also do this (max length 4 in example):

\b(x{1}y{1}|x{2}y{2}|x{3}y{3}|x{4}y{4})\b

https://regex101.com/r/vMl8vv/2

but this looks ugly and will result in a massive regex when the length is big.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=279106&siteId=1