Regular Expression to Add Space After 10 Digits

8,144

you need to use Capture groups if you want to perform replacement operations while preserving components of the original string.

your regex would follow something like:

pattern: "^([\d]{10})([\d]{10})" //two groups of 10

replace: "$1 $2"

The parentheses around each pattern for a block of ten, define a capture group. so we are saying, "the first ten digits in group $1, and the second 10 are in group $2". then we just say "print group 1, add a space, and print group 2".

Note that each platform is different in terms of its Regex syntax and replace syntax. many use $number (like sed), others use \number (like notepad++), so be sure to consult the documentation for your product.

Share:
8,144

Related videos on Youtube

THE DOCTOR
Author by

THE DOCTOR

Updated on September 18, 2022

Comments

  • THE DOCTOR
    THE DOCTOR over 1 year

    How would I go about writing a regular expression to do a find/replace operation in order to add a space after 10 digits of ever number that is 20 digits long?

    I have a long list of phone numbers where some of them are accidentally 20 digits long and I need to add a space in between each instance of that.

    Edit: I tried \d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d which selects 20 digit numbers, but replacing that with a space will just replace all 20 digits with blank whitespace rather than inserting a space after 10 digits.

    I am attempting to do this with Sublime Text on a MacBook Pro.

    • THE DOCTOR
      THE DOCTOR over 7 years
      Modified my question and added what I have tried so far and what I am using for this.