Regular Expression Examples

A number of visitors have come to my website using the search terms regex replace. So I thought I would devote an entire article on how to use regular expressions to do a find and replace on a string in some popular languages. Example code is always attractive so lets get to the point! There is example code in Ruby, Perl, Python, Javascript, and Java. [If you have other suggestions let me know or show me in your comments!]

All of the basic examples:

  1. put the string “one two three” into a variable
  2. then use a regular expression and a native function to the language to
  3. transform the original variable’s value to the new string “one 2 three”

Click Here For the Basic Examples

Now you may recognize that in the above examples that regular expressions where not even needed. All we did was find and replace a string and that simple task can be done without regular expressions! So here is a more advanced example without the training wheels.

In the advanced examples:

  1. the string “a1b2c3″ [may not need to be stored in a variable] is
  2. manipulated by a [globally replacing] regular expression
  3. resulting in “a11b22c33″ [where all numbers, but not letters, are duplicated]
  4. which is stored in a variable

Click Here To Toggle the Advanced Examples

Ruby:

result = 'a1b2c3'.gsub( /(\d)/, '\1\1' )

Perl:

$result = 'a1b2c3';
$result =~ s/(\d)/\1\1/g;

Python:

import re
result = re.sub(r'(\d)', '\\1\\1', 'a1b2c3')

Javascript:

var result = 'a1b2c3'.replace( /(\d)/g, "$1$1" );

Java:

public class RegexTest {
  public static void main(String args[]) {
    String str = "a1b2c3";
    String result = str.replaceAll("(\\d)", "$1$1");
  }
}

Pay strict attention to the number of backslashes required in python, the $1 used in Java and Javascript (however these are also global variables found in Ruby and Perl), and the trailing /g option required in Perl and Javascript for the global replacement. Each language has its own little spin on things.

I hope this helped answer your questions on regular expressions. In case I whet your appetite on Regular Expressions I can point you to my Introductory Article on Regular Expressions and my command line utility rr that allows you to run Ruby regular expression find and replace commands on files, standard input, and even piped input.

2 Responses

1

Liza on April 24, 2009 at 6:00 am  #

I can tell that this is not the first time at all that you write about this topic. Why have you chosen it again?

2

Joseph Pecoraro on April 24, 2009 at 9:19 am  #

@Liza: I think that all developers should learn regular expressions. They are an excellent skill to know, and there is really no excuse not to know them. I wrote on that in the past, although I probably could do a better job arguing for learning regex now then I did then.

The problem this article is addressing is that: you may already know regular expressions, but every language handles them slightly differently. Here is a reference so you can quickly recall that “oh yah Javascript uses $1 but Ruby does \1.” There are small gotchas in each language. This has helped me once in a while when I forget how a certain language handles things.

Add a Comment

search