0

I would like to replace from a number of 16 digits, it's 5th to 10th digit.

How can that be achieved with a regular expression (C#)?

10
  • A non-regex solution would probably be better here, since your dealing with digit positions. Related: stackoverflow.com/questions/11328353/… Commented Jul 14, 2014 at 15:47
  • How about you show us what you've got so far? Commented Jul 14, 2014 at 15:47
  • @DGibbs: You want to see his "guess?" Commented Jul 14, 2014 at 15:48
  • Regular Expressions are incredibly powerful, but they should not be used as the solution every single time you need to manipulate some text. Often other ways are easier less processing intensive. Commented Jul 14, 2014 at 15:49
  • Why not just pluck out the digits you want to replace and create a new value from it? Regular expressions seems like unnecessary overkill. Commented Jul 14, 2014 at 15:49

4 Answers 4

3

The way to do it is to capture in the inner and outer portions separately, like this:

 // Split into 2 groups of 5 digits and 1 of 6
 string regex = "(\\d{5})(\\d{5})(\\d{6})";

 // Insert ABCDEF in the middle of 
 // match 1 and match 3
 string replaceRegex = "${1}ABCDE${3}";

 string testString = "1234567890999999";

 string result = Regex.Replace(testString, regex, replaceRegex);

 // result = '12345ABCDE999999'
Sign up to request clarification or add additional context in comments.

3 Comments

As an aside, the first line would read a tiny bit better like this: string regex = @"(\d{5})(\d{5})(\d{6})";
Its compact as you are declaring everything inline ;-)
@DavidG: I was going to type that just now, but thought this was albeit a bit more verbose, clearer in what I was trying to show. I could do a one liner ;-)
2

Why use a regular expression? If by "number of 16 digits", you mean a 16 character long string representation of a number, then you'd probably be better off just using substring.

string input = "0000567890000000"; 
var output = input.Substring(0, 4) + "222222" + input.Substring(10, 6);

Or did you mean you want to swap the 5th and 10th digits? Your question isn't very clear.

Comments

0

Use the regular expression (?<=^\d{4})\d{6}(?=\d{6}$) to achieve it without capture groups.

It looks for 6 consecutive digits (5th to 10th inclusively) that are preceded by the first 4 digits and the last 6 digits of the string.

Regex.Replace("1234567890123456", @"(?<=^\d{4})\d{6}(?=\d{6}$)", "replacement");

Comments

0

Got it... by creating 3 capturing groups:

([\d]{5})([\d]{5})([\d]{6})

keep capturing group1 and 3 and replace group2 with stars (or whatever)

$1*****$3

C# code below

string resultString = null;
try {
resultString = Regex.Replace(subjectString, @"([\d]{5})([\d]{5})([\d]{6})", "$1*****$2", RegexOptions.Singleline);
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}

6 Comments

Notice that you are using 4 + 5 + 6 = 15 characters, not 16 as per your original question
this is the solution i just put together from regex buddy.
explained more above, and updated with 16 characters, my fault on the typo.
Isn't that what I proposed as a solution? Why ignore casing? ;-)
you are right, removed it. Marked yours as a solution.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.