Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6912509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:06:04+00:00 2026-05-27T09:06:04+00:00

I am a regex supernoob (just reading my first articles about them), and at

  • 0

I am a regex supernoob (just reading my first articles about them), and at the same time working towards stronger use of vim. I would like to use a regex to search for all instances of a colon : that are not followed by a space and insert one space between those colons and any character after them.

If I start with:

foo:bar

I would like to end with

foo: bar

I got as far as %s/:[a-z] but now I don’t know what do for the next part of the %s statement.

Also, how do I change the :[a-z] statement to make sure it catches anything that is not a space?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:06:05+00:00Added an answer on May 27, 2026 at 9:06 am

    :%s/:\(\S\)/: \1/g

    \S matches any character that is not whitespace, but you need to remember what that non-whitespace character is. This is what the \(\) does. You can then refer to it using \1 in the replacement.

    So you match a :, some non-whitespace character and then replace it with a :, a space, and the captured character.


    Changing this to only modify the text when there’s only one : is fairly straight forward. As others have suggested, using some of the zero-width assertions will be useful.

    :%s/:\@!<:[^:[:space:]]\@=/: /g

    • :\@!< matches any non-:, including the start of the line. This is an important characteristic of the negative lookahead/lookbehind assertions. It’s not requiring that there actually be a character, just that there isn’t a :.

    • : matches the required colon.

    • [^:[:space:]] introduces a couple more regex concepts.

      • The outer [] is a collection. A collection is used to match any of the characters listed inside. However, a leading ^ negates that match. So, [abc123] will match a, b, c, 1, 2, or 3, but [^abc123] matches anything but those characters.

      • [:space:] is a character class. Character classes can only be used inside a collection. [:space:] means, unsurprisingly, any whitespace. In most implementations, it relates directly to the result of the C library’s isspace function.

      Tying that all together, the collection means “match any character that is not a : or whitespace”.

    • \@= is the positive lookahead assertion. It applies to the previous atom (in this case the collection) and means that the collection is required for the pattern to be a successful match, but will not be part of the text that is replaced.

    So, whenever the pattern matches, we just replace the : with itself and a space.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

regex not working as wanted Code example: widgetCSS = #widgetpuffimg{width:100%;position:relative;display:block;background:url(/images/small-banner/Dog-Activity-BXP135285s.jpg) no-repeat 50% 0; height:220px;}
What REGEX should I use to extract the following parameter from a url string:
var REGEX = new RegExp((?=[hms])g) var TIME = _GET('t', '0').split(REGEX) var hours = TIME[TIME.indexOf('h')-1]
With Regex::Replace we can use $1, $2, ... to match corresponding groups. But how
What regex can I use to match .#,#. within a string. It may or
My regex skills are pretty poor, and most of the time they make me
My regex was working - until the form of the string it was capturing
What regex pattern should I use to find two strings with a similar value
What regex code can i use to find an html tag, and then extract
Regex newbie here. There's a (broken) plugin for the forum software I use that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.