# If You Like Regular Expressions So Much, Why Don't You Marry Them?

**URL:** <https://discourse.codinghorror.com/t/if-you-like-regular-expressions-so-much-why-dont-you-marry-them/1150>\
**Category:** blog\
**Created:** [March 22, 2005, 12:00am UTC](https://discourse.codinghorror.com/t/if-you-like-regular-expressions-so-much-why-dont-you-marry-them/1150 "2005-03-22T00:00:00Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![codinghorror](https://discourse-cdn.codinghorror.com/user_avatar/discourse.codinghorror.com/codinghorror/32/36371_2.png) [@codinghorror](https://discourse.codinghorror.com/u/codinghorror)\
**Post date:** [March 22, 2005, 12:00am UTC](https://discourse.codinghorror.com/t/if-you-like-regular-expressions-so-much-why-dont-you-marry-them/1150/1 "2005-03-22T00:00:00Z")

</div>

[All right... _I will!_](http://www.x-entertainment.com/messages/509.html)

I'm continually amazed how useful regular expressions are in my daily coding. I'm still working on the MhtBuilder refactoring, and I needed a function to convert all URLs in a page of HTML from relative to absolute:

* * *
This is a companion discussion topic for the original blog entry at: [http://www.codinghorror.com/blog/2005/03/if-you-like-regular-expressions-so-much-why-dont-you-marry-them.html](http://www.codinghorror.com/blog/2005/03/if-you-like-regular-expressions-so-much-why-dont-you-marry-them.html)

---

<div class="post-metadata">

**Author:** ![codinghorror](https://discourse-cdn.codinghorror.com/user_avatar/discourse.codinghorror.com/codinghorror/32/36371_2.png) [@codinghorror](https://discourse.codinghorror.com/u/codinghorror)\
**Post date:** [March 23, 2005, 12:00am UTC](https://discourse.codinghorror.com/t/if-you-like-regular-expressions-so-much-why-dont-you-marry-them/1150/2 "2005-03-23T00:00:00Z")

</div>

We use TOAD (only for ORACLE dbs), which has an entire module dedicated to this…

[http://www.quest.com/toad/](http://www.quest.com/toad/)

---

<div class="post-metadata">

**Author:** ![codinghorror](https://discourse-cdn.codinghorror.com/user_avatar/discourse.codinghorror.com/codinghorror/32/36371_2.png) [@codinghorror](https://discourse.codinghorror.com/u/codinghorror)\
**Post date:** [March 23, 2005, 12:00am UTC](https://discourse.codinghorror.com/t/if-you-like-regular-expressions-so-much-why-dont-you-marry-them/1150/3 "2005-03-23T00:00:00Z")

</div>

but there are potential performance issues in recompiling the regex on each postback

They could just embed the precompiled regex into their assembly, but at the framework level of the coding pyramid it probably makes more sense to hard-code it.

---

<div class="post-metadata">

**Author:** ![mike245](https://discourse-cdn.codinghorror.com/letter_avatar_proxy/v2/letter/m/838e76/32.png) [@mike245](https://discourse.codinghorror.com/u/mike245)\
**Post date:** [March 23, 2005, 12:00am UTC](https://discourse.codinghorror.com/t/if-you-like-regular-expressions-so-much-why-dont-you-marry-them/1150/4 "2005-03-23T00:00:00Z")

</div>

[… did you know that the [ASP.NET](http://ASP.NET) page parser uses regular expressions? …]

And another trivium is that the ValidateRequest logic that looks for HTML and other “potentially dangerous” markup in the postback does NOT use regexes. It could – it’s a natural application – but there are potential performance issues in recompiling the regex on each postback.

I have a few details about that here, including the hypothetical regex that they would use:

[http://mikepope.com/blog/DisplayBlog.aspx?permalink=441](http://mikepope.com/blog/DisplayBlog.aspx?permalink=441)

---

<div class="post-metadata">

**Author:** ![KMF](https://discourse-cdn.codinghorror.com/letter_avatar_proxy/v2/letter/k/9f8e36/32.png) [@KMF](https://discourse.codinghorror.com/u/KMF)\
**Post date:** [March 23, 2005, 12:00am UTC](https://discourse.codinghorror.com/t/if-you-like-regular-expressions-so-much-why-dont-you-marry-them/1150/5 "2005-03-23T00:00:00Z")

</div>

I have been looking for a tool to beautify SQL for quite some time. Where could I find them?

---

<div class="post-metadata">

**Author:** ![Erica4](https://discourse-cdn.codinghorror.com/letter_avatar_proxy/v2/letter/e/49beb7/32.png) [@Erica4](https://discourse.codinghorror.com/u/Erica4)\
**Post date:** [April 22, 2005, 12:00am UTC](https://discourse.codinghorror.com/t/if-you-like-regular-expressions-so-much-why-dont-you-marry-them/1150/6 "2005-04-22T00:00:00Z")

</div>

Anyone got a regex that converts an absolute path into a relative path? I need to convert “[http://domain.com/folder/images/myimage.gif](http://domain.com/folder/images/myimage.gif)” to just “images/myimage.gif”.

---

<div class="post-metadata">

**Author:** ![codinghorror](https://discourse-cdn.codinghorror.com/user_avatar/discourse.codinghorror.com/codinghorror/32/36371_2.png) [@codinghorror](https://discourse.codinghorror.com/u/codinghorror)\
**Post date:** [April 22, 2005, 12:00am UTC](https://discourse.codinghorror.com/t/if-you-like-regular-expressions-so-much-why-dont-you-marry-them/1150/7 "2005-04-22T00:00:00Z")

</div>

Erica,

Here are a few… starting with:

[http://domain.com/folder/images/myimage.gif](http://domain.com/folder/images/myimage.gif)

Return the last filename in the url:

“[^/]+[^/]$” – myimage.gif

Return the last folder in the url:

“[^/]+/(?=$|[^/]+$)” – images/

Return the webroot:

“^\w+://[^/]+(/)\*” – [http://domain.com/](http://domain.com/)

Return the webroot plus the first subfolder:

“^\w+://([^/]+/){2}” – [http://domain.com/folder/](http://domain.com/folder/)

The problem you run into is that relative is… uh… relative to what?

---

<div class="post-metadata">

**Author:** ![Samir](https://discourse-cdn.codinghorror.com/letter_avatar_proxy/v2/letter/s/ea666f/32.png) [@Samir](https://discourse.codinghorror.com/u/Samir)\
**Post date:** [March 27, 2006, 12:00am UTC](https://discourse.codinghorror.com/t/if-you-like-regular-expressions-so-much-why-dont-you-marry-them/1150/8 "2006-03-27T00:00:00Z")

</div>

Sorry , I 4got to add this information.All the links are of the form:  
"a href=“disable\_javascript:mapWindow=x\_window.open(’/something.htm’)”

I need to convert these relative urls to absolute ones. I know that i have to use the Replace() method.How do i go about it??  
I went thru the code that is posted above and didnt understand this line:  
“html = r.Replace(html, “${attrib}=${delim1}” \_HtmlFile.UrlRoot “/${url}${delim2}”)”

What does \_HtmlFile.UrlRoot mean??

---

<div class="post-metadata">

**Author:** ![S\_Jan](https://discourse-cdn.codinghorror.com/letter_avatar_proxy/v2/letter/s/48db29/32.png) [@S\_Jan](https://discourse.codinghorror.com/u/S_Jan)\
**Post date:** [June 14, 2006, 12:00am UTC](https://discourse.codinghorror.com/t/if-you-like-regular-expressions-so-much-why-dont-you-marry-them/1150/9 "2006-06-14T00:00:00Z")

</div>

Hi,  
I have got the same problem as samir.  
Samir did you or anyone else found the meaning of \_HtmlFile.UrlRoot?

---

<div class="post-metadata">

**Author:** ![Tina](https://discourse-cdn.codinghorror.com/letter_avatar_proxy/v2/letter/t/48db29/32.png) [@Tina](https://discourse.codinghorror.com/u/Tina)\
**Post date:** [July 9, 2007, 12:00am UTC](https://discourse.codinghorror.com/t/if-you-like-regular-expressions-so-much-why-dont-you-marry-them/1150/10 "2007-07-09T00:00:00Z")

</div>

Hi guys,  
I have a regular expression proble, I have a huge html text and I want to convert all a hreftext/a to text where the link does not start with http://  
In short I want only external links in my document and want to replace others with their respective texts.

Please please help

---

<div class="post-metadata">

**Author:** ![JuanZ](https://discourse-cdn.codinghorror.com/letter_avatar_proxy/v2/letter/j/bbce88/32.png) [@JuanZ](https://discourse.codinghorror.com/u/JuanZ)\
**Post date:** [June 27, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/if-you-like-regular-expressions-so-much-why-dont-you-marry-them/1150/11 "2008-06-27T00:00:00Z")

</div>

[http://www.salsasetc.com/graphics/H-175A%20large.jpg](http://www.salsasetc.com/graphics/H-175A%20large.jpg)

In reference to:  
[http://www.codinghorror.com/blog/archives/001016.html](http://www.codinghorror.com/blog/archives/001016.html)

---

<div class="post-metadata">

**Author:** ![cowgod](https://discourse-cdn.codinghorror.com/letter_avatar_proxy/v2/letter/c/e56c9b/32.png) [@cowgod](https://discourse.codinghorror.com/u/cowgod)\
**Post date:** [June 27, 2008, 12:00am UTC](https://discourse.codinghorror.com/t/if-you-like-regular-expressions-so-much-why-dont-you-marry-them/1150/12 "2008-06-27T00:00:00Z")

</div>

it’s only about three years too late, but KMF, here’s an awesome sql formatter:

[http://www.sqlinform.com/](http://www.sqlinform.com/)

maybe others will find it useful even though this post is quite old.

---

<div class="post-metadata">

**Author:** ![Tris](https://discourse-cdn.codinghorror.com/user_avatar/discourse.codinghorror.com/tris/32/77696_2.png) [@Tris](https://discourse.codinghorror.com/u/Tris)\
**Post date:** [December 24, 2022, 4:19pm UTC](https://discourse.codinghorror.com/t/if-you-like-regular-expressions-so-much-why-dont-you-marry-them/1150/13 "2022-12-24T16:19:01Z")

</div>

> [@codinghorror](#):
>
> [recommended pretty-printing regular expressions](http://blogs.msdn.com/bclteam/archive/2005/03/15/396450.aspx),

It looks like that link might be here now: [.NET Regular Expressions: how to use RegexOptions.IgnorePatternWhitspace [Ryan Byington] | Microsoft Learn](https://learn.microsoft.com/en-us/archive/blogs/bclteam/net-regular-expressions-how-to-use-regexoptions-ignorepatternwhitspace-ryan-byington)
