BearPanther.com

Sublime Text’s Search and Replace with Regular Expressions

Introduction

user

bear


LATEST POSTS

Using jsformat with Sublime Text 26th July, 2014

There are many d3 demos like this but this one is mine 08th June, 2014

Blog

Sublime Text’s Search and Replace with Regular Expressions

Posted on .

I know I’m not alone when I say I like code formatted a particular way. And that I spend more time that I’d care to admit converting tabs to spaces, indenting, un-indenting and so on.

Lately I’ve been using Sublime Text 2’s find and replace with regular expressions to speed up getting code looking just right. One specific thing I’ve been doing is using a regex to put a space between the colon after CSS property and its value. Let’s say we have the following CSS:

html, body {
  font-family: "Lucida Grande", "Segoe UI", Arial;
  font-size: 1.2em;
  height:100%;
  margin:0;
  padding:0;
  width:100%;
}

See the problem? The font properties are good but the rest are scrunched together. Totally unacceptable. Here’s how I’ve been fixing it lately:

In the “Find What:” box we’re telling Sublime to look for a colon followed by any alphanumeric character as well as the “#” symbol with this regular expression:

:([a-zA-z0-9#])

Note that the alphanumeric + “#” part is in parentheses which says that matches should be treated as a group. By doing that, the matching group can be referenced as $1 in the “Replace With:” box.

To update each CSS property to have a space after the colon, in the “Replace With:” box, use:

: $1

Then click “Replace All”. Not only do I feel better about the formatting for this particular chunk of CSS, I can get back to doing real work.

Comments
user

Author parhum

Posted at 11:03 pm May 10, 2014.

Thanks, I have a question;
I want to find:
????
and replace with
MY Custom URL

What can I do?

Reply
user

Author Carl Manaster

Posted at 8:07 am June 19, 2014.

Another less regexy way is to replace all “: ” with “:” then replace all “:” with “: “.

Reply
user

Author phoenix sell jewelry

Posted at 4:44 pm July 13, 2014.

Terrific article! That is the kind of info that should be
shared around the web. Disgrace on Google for now not positioning this
publish higher! Come on over and discuss with my site .
Thanks =)

Reply
user

Author acura layton utah

Posted at 2:48 pm August 4, 2014.

Hmm is anyone else having problems with the pictures on this blog loading?
I’m trying to figure out if its a problem on my end or if
it’s the blog. Any responses would be greatly
appreciated.

Reply
user

Author GuyS

Posted at 10:03 am November 29, 2016.

YES! I know, it’s the simple things. But that group $1 is the key. I was trying to tab-delimit an horribly formatted log file and needed to add a tab before every data/time stamp entry. Regex on ” ([0-9][0-9]:)” (space after the ” before the numbers) and replace with “\t$1” and I’m in business! Thanks much.

Reply
user

Author Lavonne1992

Posted at 5:06 pm May 28, 2017.

This post is on 16 spot in google’s search results, if you want more visitors, you should build more backlinks to your blog, there is one trick to get free, hidden backlinks from authority forums, search
on youtube: how to get hidden backlinks from forums

Reply
user

Author JefferyPerl

Posted at 1:17 am June 20, 2017.

I see your blog needs some unique & fresh articles.
Writing manually is time consuming, but there is
solution for this hard task. Just search for: Miftolo’s tools rewriter

Reply

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

View Comments (8) ...