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. Regex to find out length

Regex to find out length

Scheduled Pinned Locked Moved Regular Expressions
questionregex
3 Posts 3 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.
  • R Offline
    R Offline
    RanCohen
    wrote on last edited by
    #1

    Lets say I want to "encrypt" the ID number 123456789 - add '*' to all the digits up to the last four - *****6789. A starting point is to replace .*(....) with *$1. But it would produce *6789, which is too short. How can I know how many chars the first .* matched?

    Richard DeemingR G 2 Replies Last reply
    0
    • R RanCohen

      Lets say I want to "encrypt" the ID number 123456789 - add '*' to all the digits up to the last four - *****6789. A starting point is to replace .*(....) with *$1. But it would produce *6789, which is too short. How can I know how many chars the first .* matched?

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

      It depends which language and framework you're using. But a regex is almost certainly the wrong tool for this job. For example, in C# you might use something like this:

      string input = "1234567890";

      string output = string.Create(input.Length, input, static (buffer, input) =>
      {
      int pivot = buffer.Length - 4;
      buffer[0..pivot].Fill('*');
      input.AsSpan()[pivot..].CopyTo(buffer[pivot..]);
      });


      "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

      1 Reply Last reply
      0
      • R RanCohen

        Lets say I want to "encrypt" the ID number 123456789 - add '*' to all the digits up to the last four - *****6789. A starting point is to replace .*(....) with *$1. But it would produce *6789, which is too short. How can I know how many chars the first .* matched?

        G Offline
        G Offline
        Graham Breach
        wrote on last edited by
        #3

        You could use a zero-length lookahead.

        "1234345242342356789".replaceAll(/\d(?=\d{4})/g, "*")

        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