Match if token exists only power of n times; Match if length of string is power of n
-
Hi, I was trying to make a regex that matches only if x is present power of 2 times (n=2). Asked otherwise, make a regex that matches if the length of the string is the power of 2 where the only character in the string is x in this case. So for example, xx (2), xxxx (4), xxxxxxxx (8) should match but xxx (3), xxxxxx (6) should not match. Besides this, I also have a query on the regex I was trying to make:
^((xx)*){2}$
to match xx, xxxx and not xxxxxx but this matches the later (see here). From the debugger also, I cannot understand why last 2 x in 6 x matches with the inner group when we have {2} outside.
-
Hi, I was trying to make a regex that matches only if x is present power of 2 times (n=2). Asked otherwise, make a regex that matches if the length of the string is the power of 2 where the only character in the string is x in this case. So for example, xx (2), xxxx (4), xxxxxxxx (8) should match but xxx (3), xxxxxx (6) should not match. Besides this, I also have a query on the regex I was trying to make:
^((xx)*){2}$
to match xx, xxxx and not xxxxxx but this matches the later (see here). From the debugger also, I cannot understand why last 2 x in 6 x matches with the inner group when we have {2} outside.
A regular expression is the wrong tool for this. Unless this is a homework assignment or coding challenge, there will be far simpler ways to test whether the length of a string is a power of 2.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
A regular expression is the wrong tool for this. Unless this is a homework assignment or coding challenge, there will be far simpler ways to test whether the length of a string is a power of 2.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I know I know. Yes, it is a challenge!