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. C#
  4. Regex replace exact 5 digit not more than that

Regex replace exact 5 digit not more than that

Scheduled Pinned Locked Moved C#
cssregex
5 Posts 4 Posters 0 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.
  • S Offline
    S Offline
    shakumar_21
    wrote on last edited by
    #1

    Hi I want to replace only 5 digit (not less than or not more than) to “xxxxx” with Regex I tried - InputString = "sample1: 12345 – sample2 : 123456789" Dim regExp As Regex = New Regex("(\d{5})") regExp.Replace(InputString, "xxxxx") output - "sample1: xxxxx – sample2 : xxxxx6789" But i want as "sample1: xxxxx – sample2 : 123456789" if more than 5 digit , it must not replace.

    OriginalGriffO 1 Reply Last reply
    0
    • S shakumar_21

      Hi I want to replace only 5 digit (not less than or not more than) to “xxxxx” with Regex I tried - InputString = "sample1: 12345 – sample2 : 123456789" Dim regExp As Regex = New Regex("(\d{5})") regExp.Replace(InputString, "xxxxx") output - "sample1: xxxxx – sample2 : xxxxx6789" But i want as "sample1: xxxxx – sample2 : 123456789" if more than 5 digit , it must not replace.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Add a whitespace and an "end-of-line" indicator:

      Dim regExp As Regex = New Regex("\s(\d{5})$")

      And include the whitespace back in your replacement string. And in future: post VB in the VB forum, and C# in the C# forum?

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      S 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Add a whitespace and an "end-of-line" indicator:

        Dim regExp As Regex = New Regex("\s(\d{5})$")

        And include the whitespace back in your replacement string. And in future: post VB in the VB forum, and C# in the C# forum?

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        S Offline
        S Offline
        sudabeh3
        wrote on last edited by
        #3

        It was exactly a C# question and not VB. I wonder why this simple question have made all nervous! it was related to Fuzzy Logic and Fuzzy Expert Systems. Anyway, thanks a lot for your all answers.

        P OriginalGriffO 2 Replies Last reply
        0
        • S sudabeh3

          It was exactly a C# question and not VB. I wonder why this simple question have made all nervous! it was related to Fuzzy Logic and Fuzzy Expert Systems. Anyway, thanks a lot for your all answers.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          You have replied to someone else's question. The question that was asked here was written in VB.

          1 Reply Last reply
          0
          • S sudabeh3

            It was exactly a C# question and not VB. I wonder why this simple question have made all nervous! it was related to Fuzzy Logic and Fuzzy Expert Systems. Anyway, thanks a lot for your all answers.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            If you look at the question I answered, you will see it is written in VB, not C#...

            Dim regExp As Regex = New Regex("(\d{5})")

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            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