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. A Regular Expression that will search and replace characters from a string

A Regular Expression that will search and replace characters from a string

Scheduled Pinned Locked Moved Regular Expressions
regexhelptutorialquestion
4 Posts 3 Posters 3 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.
  • U Offline
    U Offline
    User 13540679
    wrote on last edited by
    #1

    Hello Community, I'm trying to compile a regular expression that will search for strings that exclude certain characters. For example, the following string value has 6 leading 0's 000000120 The next string excludes the 0's 121 122 I would like a regular expression that can find strings without the leading 0's and then add the 0's to it. Therefore, 121, and 122 would become 000000121 and 000000122. Can you help with this? Thanks Carlton

    Richard DeemingR G 2 Replies Last reply
    0
    • U User 13540679

      Hello Community, I'm trying to compile a regular expression that will search for strings that exclude certain characters. For example, the following string value has 6 leading 0's 000000120 The next string excludes the 0's 121 122 I would like a regular expression that can find strings without the leading 0's and then add the 0's to it. Therefore, 121, and 122 would become 000000121 and 000000122. Can you help with this? Thanks Carlton

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

      How about:

      \b[1-9]\d*\b

      Demo[^] Adding the correct number of leading zeros will depend on the language you're using. For example, in C#:

      string output = Regex.Replace(input, @"\b[1-9]\d*\b", match => match.Value.PadLeft(9, '0'));

      In Javascript:

      const output = input.replace(/\b[1-9]\d*\b/g, match => match.padStart(9, '0'));

      Demo[^]


      "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

      U 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        How about:

        \b[1-9]\d*\b

        Demo[^] Adding the correct number of leading zeros will depend on the language you're using. For example, in C#:

        string output = Regex.Replace(input, @"\b[1-9]\d*\b", match => match.Value.PadLeft(9, '0'));

        In Javascript:

        const output = input.replace(/\b[1-9]\d*\b/g, match => match.padStart(9, '0'));

        Demo[^]


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

        U Offline
        U Offline
        User 13540679
        wrote on last edited by
        #3

        This is fantastic. However, my platform uses javase and the following regex was able to pick out the strings without leading 0's e.g. 121 and 122. However, the regex "(9, '0')" doesn't replace 121, and 122 with 000000121 and 000000122. Nevertheless, this is great. \b[1-9]\d*\b

        1 Reply Last reply
        0
        • U User 13540679

          Hello Community, I'm trying to compile a regular expression that will search for strings that exclude certain characters. For example, the following string value has 6 leading 0's 000000120 The next string excludes the 0's 121 122 I would like a regular expression that can find strings without the leading 0's and then add the 0's to it. Therefore, 121, and 122 would become 000000121 and 000000122. Can you help with this? Thanks Carlton

          G Offline
          G Offline
          GenJerDan
          wrote on last edited by
          #4

          Does it have to be regex? There's

          StringUtils.leftPad ()

          if you want to pad with leading 0s, as long as you know the total length you want. Or use a format string to do it.

          String paddedStr = String.format("%09d", originalVal);

          (I think)

          We won't sit down. We won't shut up. We won't go quietly away. YouTube, and My Mu[sic], Films and Windows Programs, etc. and FB

          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