Regular Expressions, Reverend Jim Ignatowski and You

Posted: July 30th, 2010 | Author: | Filed under: Regular Expressions | 4 Comments »

Regular Expressions

I use regular expressions just often enough to have to totally re-learn them every time I need to use them. After a round of Google searches, reading over a few good regex sites (here’s one) and a bit of head scratching and code tinkering I end up with the code I need.

In this particular case I needed append a variable with punctuation in cases where the variable ended in an alphanumeric character. Regex to the rescue!

1
2
3
4
5
6
7
<cfset variables.question = "what does the yellow light mean" />
<cfif ReFindNoCase("[a-zA-Z0-9]$", variables.question )>
     <cfset variables.question = variables.question & "?" />
</cfif>
<cfoutput> 
     #variables.question #
</cfoutput>

Reverend Jim Ignatowski

This has nothing to do with regular expressions, but it’s a classic moment from Taxi.

You

The next time you need some regex help you may want to check out http://www.regular-expressions.info. If you have any good regex tips or sites leave them in the comments below. As usual, spammers will be shot on site.


4 Comments on “Regular Expressions, Reverend Jim Ignatowski and You”

  1. 1 Peter Boughton said at 12:21 am on July 31st, 2010:

    If you’re doing refindnocase you don’t need both A-Z and a-z in the class.

    Also, another way of doing it is this (no need for the if):

    variables.question = rereplaceNoCase(“[a-z0-9]$”, variables.question , “?” )

    i.e. if the last char is alphanumeric, perform the replace, where is the match (so the last char), and the ? is appended after it.

  2. 2 Peter Boughton said at 12:24 am on July 31st, 2010:

    Looks like the \ (backslash,zero) has been eaten by the blog software.
    The replace string should be “\?” (backslash,zero,questionmark) and the the “where is the match” should say “where \ is the match” – i.e. a single backslash followed by zero.

  3. 3 Peter Boughton said at 12:26 am on July 31st, 2010:

    Looks like the \\ 0 (backslash,zero) has been eaten by crappy WordPress.
    The replace string should be “\\ 0?” (backslash,zero,questionmark) and the the “where is the match” should say “where \\ 0 is the match” – i.e. a single backslash followed by zero.
    There should be no space between the backlash and the zero.

  4. 4 Christopher Vigliotti said at 6:15 pm on September 3rd, 2010:

    Hi Peter, sorry for the delay on approving your comments. Good point about leaving swapping out a-zA-Z with a-z in the code sample.

    Just so I understand your second point (sorry about the WordPress issue), you are saying that I should replace the ‘questionmark’ with ‘questionmark backslash zero’?

    Thanks for your input/suggestions.


Leave a Reply