Use a regular expression to match a string up to a given character

Often I run into a string where all I want is to match everything up to a given character. So given the following test string:

This is my string with a pipe | and everything after it is stupid.

Using Ruby as my reference language, I might do something like this in an interactive ruby session (aka: IRB) or on Rubular.com:

str = 'This is my string with a pipe | and everything after it is stupid.'
str.match(/^.*(?=\|)/).first

Which would produce the following in Ruby:

"This is my string with a pipe "

Lets break down the different parts of the expression ^.*(?=\|) and what they're doing:

  • ^ start at the beginning of the string
  • .* match anything from the current position
  • (?=\|) only if followed by a pipe character

The last part of the expression is what's known as a positive look-ahead (?=). Using a look-ahead is like attaching a condition to an existing regular expression. I'm telling my expression to return the match only if it's followed by characters matched by an additional expression.

For a more comprehensive dive into look-around functions, head on over to Regular-Expressions.info. Also feel free to play with my example on Rubular.com.

As always, use regular expressions responsibly.

November 20, 2015