Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Regular Expressions
  4. Match if token exists only power of n times; Match if length of string is power of n

Match if token exists only power of n times; Match if length of string is power of n

Scheduled Pinned Locked Moved Regular Expressions
regexdatabasecomdebuggingtutorial
3 Posts 2 Posters 11 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_15562196
    wrote on last edited by
    #1

    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.

    Richard DeemingR 1 Reply Last reply
    0
    • M Member_15562196

      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.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        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

        M Offline
        M Offline
        Member_15562196
        wrote on last edited by
        #3

        I know I know. Yes, it is a challenge!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups