scriptygoddess

13 Jan, 2009

Reg Ex samples

Posted by: Jennifer In: Regular Expressions

I've been reading up on Reg Ex and just STARTING to get my head around the subject. One use I've found for it immediately is with Dreamweaver. (Dreamweaver has a "regular expression" search option). Just for my own future re-use and storage – I'll post a few I've used. (Will probably/hopefully add to this post over time…)

<td><a href=["].+["] target=["]_blank["]></a></td>
searches for
<td><a href="ANYTHING CAN GO HERE" target="_blank"></a></td>

<[/]*tr>
searches for
<tr> and </tr>

To replace only PORTIONS of what you find –
search for:
<td><a href=["](.+)["] target=["]_blank["]>
and then you can replace lit like this
<td><a href=["]$1["]>
(will "save" anything it found with (.+) and keep it in the replace with $1

Used this one today (6/5/09) – had to find spaces in a query string and replace it with %20
(pagename.php[?]m=\S[^"]*)[\s]
That will find pagename.php?m=anything except the ending quote
It stores everything except the space (should be the last one in the query string) in $1 so you can do a replace with:
$1%20

New one (8/14):
<!–.+–>
That will find all the comments on the page (again this is in dreamweaver)

Updated 7/26:
\?\>\s$$
Finds a file with space after ?> at the end of the file.

3 Responses to "Reg Ex samples"

1 | chris g

January 13th, 2009 at 4:25 pm

Avatar

This helps me a lot to get reg ex's right. http://sebastianzartner.de/firefoxExtensions/RExT/

2 | Colin Morris

January 13th, 2009 at 11:07 pm

Avatar

You might want to be careful with that first one, as it could match
<a href="stuff" rel="nofollow">good</a><a href="bad" rel="nofollow">. Admittedly it finds you what you want, but RegExp tends to be greedy. If you want to match both " and ' use ['"] rather than just ["].

Also, for just a single character in [], you can ditch the [] and go for , the [] are to group characters as options for a single character position. At least that's the normal way, I don't know if Dreamweaver does its own hoohah.

3 | William Hurford

March 23rd, 2009 at 10:45 am

Avatar

Sitepoint book by Kevin Yank is my best reminder of the key points.
I use the online php manual for the gory details.

BUT Agent Ransack is a marvelous program that composes them for you, so I seldom refer to the others anymore.

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements