Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

rename many files at once

This is where the bash variable handling makes it even more interesting. Instead of just doing something like "mv $var", we can replace text in the filename using this syntax:

${var/originaltext/replacetext}


So now, if we run this command on our directory:

for f in *;do mv $f ${f/test/prod};done

Select spacer images

Regular expression to select image tags with images whose names end in spacer.

<img src="[\w\/\-\_]*spacer.[jpeg|jpg|gif|png]+[^>]*/?>

Strip inline table and image properties

I wrote this while cleaning up some code generated by Macromedia Contribute. It put a ton of properties into images and tables

\s*(?:width="\d*")\s*(?:height="\d*")\s*(?:hspace="\d*")?\s*(?:vspace="\d*")?\s*(?:valign="\w*")?\s*(?:align="\w*")?\s*(?:align="\w*")?\s*(?:border="\d*")?

Select inline styles

Selects inline styles. I purposely excluded quotation marks from the character set because they cause the expression to select attributes of other tags on the same line.

style="[\s\d\w\.\-\:\;]+"


Edit: Here's a shorter way of accomplishing the same thing:

style="[^"']+"


It's probably worth adding a space before the expression to make sure that you don't leave in unnecessary spaces in the tag and to avoid selecting things that are not styles, such as the bold part of


<!-- fwtable fwsrc="navbar.png" fwbase="navbar.gif" fwstyle="Dreamweaver" fwdocid = "742308039" fwnested="0" -->

Select all font tags

Regular expression to select all font tags in a HTML document.

</?(FONT|font)[^>]*>

javascript phone regex

339-4248
339-42-48
339 42 48
339 4248
3394248
(095) #phone#
(095)#phone#
+7 (095) #phone#
+7 (095)#phone#
+7(095) #phone#
+7(095)#phone#

/^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/

HTML link regex

// use url group and ignore case (because of something like HREF)
// based on http://regexlib.com/REDetails.aspx?regexp_id=1048
href="(?<url>(((ht|f)tp(s?))\://)?((([a-zA-Z0-9_\-]{2,}\.)+[a-zA-Z]{2,})|((?:(?:25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)(?(\.?\d)\.)){4}))(:[a-zA-Z0-9]+)?(/[a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~]*)?)"

JS Regex's for sourcecode

Extracted from SyntaxHilighter

	MultiLineCComments : new RegExp('/\\*[\\s\\S]*?\\*/', 'gm'),
	SingleLineCComments : new RegExp('//.*$', 'gm'),
	SingleLinePerlComments : new RegExp('#.*$', 'gm'),
	DoubleQuotedString : new RegExp('"(?:\\.|(\\\\\\")|[^\\""\\n])*"','g'),
	SingleQuotedString : new RegExp("'(?:\\.|(\\\\\\')|[^\\''\\n])*'", 'g')

Visual Studio 2005 regex search & replace

// Visual Studio 2005 regex search & replace

find what = data\[{:z}\t{:z}      // :z is number, \t is tab, {} is a tagged expression
replace with = data\[\1\] =\t\2;  // \n is the nth tagged match

Bash one-liner to rename files

// bash one-liner for renaming files

for f in `find . -name '*replaceme.jpg'` ; do mv $f ${f/replaceme/withme}; done