Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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:

/\^\s*if\.*;\$

/    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
1  
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
1  
. 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

1 Answer 1

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

 
discard

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.