Jump to the next line that has a statement after an if()

1

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?

dotancohen

Posted 2012-08-08T15:40:56.253

Reputation: 9 798

1Beginning of the line is simply ^, not \^. Similarly, end of the line is simply $. – garyjohn – 2012-08-08T15:55:43.573

Thanks. The line /^\s*if\.*;$ isn't finding any patterns, either. – dotancohen – 2012-08-08T15:58:05.397

1. is a metacharacter meaning any character. \. matches only a literal .. Your code probably does not contain if. literally – RedGrittyBrick – 2012-08-08T15:59:25.443

Thank you garyjohn and RedGrittyBrick! That solved it! Please post that as an answer so that I could accept it. Thanks! – dotancohen – 2012-08-08T16:01:45.173

Answers

2

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 \(

RedGrittyBrick

Posted 2012-08-08T15:40:56.253

Reputation: 70 632

In the end I had to remove the leading backslash from the ^, ., and $ characters. Thanks! – dotancohen – 2012-08-08T16:03:38.820