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