Home > Blockchain >  Regex Help matching on specific character and position on specific string length
Regex Help matching on specific character and position on specific string length

Time:01-31

I need to build a regex that does some specific matches but I'm a novice and need some help. I need the regex to match to the 11th character only when the follow criteria are present in the string.

  1. total string is 19 characters long
  2. the first 10 characters are numeric
  3. the 11th character is 1
  4. the 12th - 19th characters are alphanumeric

I need to then replace the 11th character with an "I"

CodePudding user response:

You could use pattern: ^(\d{10})(\d)(\w{8})$

var pattern = new Regex(@"^(\d{10})(\d)(\w{8})$");
var input = "01234567891ABCD1234";
var output = pattern.Replace(input, "$1I$3");

Demo: https://dotnetfiddle.net/oTcIRa

  • Related