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. Regular Expression Find And Replace ?

Regular Expression Find And Replace ?

Scheduled Pinned Locked Moved C#
regexquestionhelptutorial
2 Posts 2 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.
  • J Offline
    J Offline
    Jacobb Michael
    wrote on last edited by
    #1

    Hi all, See the bellow lines

            string sTmp = "jacob martin (234) 890-3456 UsPhone comes -4533 UsPhone";
            string patern = @"\\(\\d{3}\\)\\s\\d{3}-\\d{4}"; //Find
            string sRpl = "XXXX";//Replace
            string sResult = "jacob martin (234) 890-XXXX UsPhone comes -4533 UsPhone";
    

    I want to replace the 4 digit elements with XXXX which is @"\(\d{3}\)\s\d{3}-\d{4}" in use formate. See there is another 4 Digit string is there (4533) but it is not in the us pattern so i have to leave that. can any one help me how to write Regular Expression for that My Question is Simple 1.STmp is the input string 2.The Reg has to find the us patern (@"\(\d{3}\)\s\d{3}-\d{4}") 3.Replace the last 4 Digts as XXXX 4.Leave the another 4 Digits which is not as valid usphone no -4533 5.The ourput Replace String should be as _"jacob martin (234) 890-XXXX UsPhone comes -4533 UsPhon_e" Please some one help me to do thanks in advance

    P 1 Reply Last reply
    0
    • J Jacobb Michael

      Hi all, See the bellow lines

              string sTmp = "jacob martin (234) 890-3456 UsPhone comes -4533 UsPhone";
              string patern = @"\\(\\d{3}\\)\\s\\d{3}-\\d{4}"; //Find
              string sRpl = "XXXX";//Replace
              string sResult = "jacob martin (234) 890-XXXX UsPhone comes -4533 UsPhone";
      

      I want to replace the 4 digit elements with XXXX which is @"\(\d{3}\)\s\d{3}-\d{4}" in use formate. See there is another 4 Digit string is there (4533) but it is not in the us pattern so i have to leave that. can any one help me how to write Regular Expression for that My Question is Simple 1.STmp is the input string 2.The Reg has to find the us patern (@"\(\d{3}\)\s\d{3}-\d{4}") 3.Replace the last 4 Digts as XXXX 4.Leave the another 4 Digits which is not as valid usphone no -4533 5.The ourput Replace String should be as _"jacob martin (234) 890-XXXX UsPhone comes -4533 UsPhon_e" Please some one help me to do thanks in advance

      P Offline
      P Offline
      Philip Tyre
      wrote on last edited by
      #2

      Use capturing groups to keep a reference to the part of the pattern you want to keep. string pattern = @"(\(\d{3}\)\s\d{3}-)\d{4}"; string sRpl = @"$1XXXX"; You can have as many groups as you like, each one being references by $1, $2, etc. You can also name the groups, but that seems like overkill for this case. :)

      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