-1

I want to add "0" at position 7

010101001
010101002
010101003
010101004

and make it look like this

0101010001
0101010002
0101010003
0101010004

I am using this regex :

(0[1-9]0[1-9]0[1-9]0[0-9][1-9])

It finds all strings of this pattern but I don't know how to point it to position 7 , like {7} but how to use it

Thanks

Gurmanjot Singh
  • 8,936
  • 2
  • 17
  • 37

3 Answers3

1

We can try matching the pattern (?<=\d{6}) and replacing with zero. The idea here is to lookbehind the string and insert zero at the seventh position, which occurs when we see six digits behind the current point.

Dim input as string = "010101004"
Dim output as string = Regex.Replace(input, "(?<=^\d{6})", "0")
Console.WriteLine(output)

0101010004

Demo

Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
0

You should be specific that you only want to look at 9 digit numbers.
If you don't do that, it turns into a rats nest of ambiguity.

Use assertion to insure only 9 digit numbers are looked at.

Based on you're regex, I suggest using this.

Find (?<!\d)((?:0[1-9]){3})(0[0-9][1-9])(?!\d)
Replace ${1}0${2}

Formatted

 (?<! \d )                     # Not a digit behind
 (                             # (1 start), first 6 qualified digits
      (?: 0 [1-9] ){3}
 )                             # (1 end)
 ( 0 [0-9] [1-9] )             # (2), last 3 qualified digits
 (?! \d )                      # Not a digit ahead
0

You can use infinite-width lookahead and lookbehind without any constraint beginning with Visual Studio Code v.1.31.0 release, and you do not need to set any options for that now.

Find What:     (?<=^\d{6})
Replace With: 0

Proof & test:

enter image description here

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397