Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A previous dev liked to put single statements on the same lines as their relevant if()s. I'm trying to find these lines now to fix them! I thought that the following regex would find them, but it isn't:


/    Start search
\^   Beginning of line
\s*  Any amount of whitespace
if   Beginning of the if() statement
\.*  Any amount of characters
;    The end of the single statement
\$   End of the line

So, where did I go wrong?

share|improve this question
Beginning of the line is simply ^, not \^. Similarly, end of the line is simply $. – garyjohn Aug 8 '12 at 15:55
Thanks. The line /^\s*if\.*;$ isn't finding any patterns, either. – dotancohen Aug 8 '12 at 15:58
. is a metacharacter meaning any character. \. matches only a literal .. Your code probably does not contain if. literally – RedGrittyBrick Aug 8 '12 at 15:59
Thank you garyjohn and RedGrittyBrick! That solved it! Please post that as an answer so that I could accept it. Thanks! – dotancohen Aug 8 '12 at 16:01
up vote 2 down vote accepted

The backslashed caret \^ matches a literal caret ^ not the start of a line

Generally characters with special meanings are called metacharacters, escaped metacharacters usually match the literal character and lose any special meaning.

Though beware of other contexts, in Perl regex ( is a capturing parenthesis metacharacter but in awk that role is given to \(

share|improve this answer
In the end I had to remove the leading backslash from the ^, ., and $ characters. Thanks! – dotancohen Aug 8 '12 at 16:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.