Find/Replace Regular Expression Anomaly

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
Jerry

Find/Replace Regular Expression Anomaly

Post by Jerry »

I wrote a Find/Replace Regular Expression to move the open curly brace ({) from the end of a line to the next line indented to line up with the first character of preceding line. But, it will not move any curly braces that are already on a line by itself. This is the search expression:

^\([ \t]*\)\([a-zA-Z0-9]+[^{]*\){[ \t]*\n

and this is the replace expression:

\1\2\n\1{\n

It works fine when I click on Replace Next and change all the occurrences one at a time. However, if I click on Replace All, it moves all the occurrences except the second line of two adjacent lines with a curly brace at the end.

For instance, using this code as an example,

public class Test {
public static void main(String[] args) {
if (args.length == 1) {
System.out.println( "arg[0] = " + arg[0] );
}
else {
System.out.println( "No args " );
}
}
}

It will move all the curly braces, except on the
public static void main(String[] args) {
line.

Why would this work doing it one line at a time using Replace Next but not using Replace All.

Thanks,
Jerry
Jerry

Re: Find/Replace Regular Expression Anomaly

Post by Jerry »

For some reason, the indentation of the code was removed when posting the message. There should be a four space indentation on each of the lines following each line with an opening curly brace and four spaces back on each line following the closing curly brace.
Berend Hasselman

Re: Find/Replace Regular Expression Anomaly

Post by Berend Hasselman »

Jerry

Replace the \n at the end of the regular expression with $.
\n should only be used to match re's that span line boundaries.
Your { occurs at the end of a line, does not span lines and therefore you should use $.
Also see the reference manual.

Berend
Berend Hasselman

Re: Find/Replace Regular Expression Anomaly

Post by Berend Hasselman »

Jerry

The replace expression should also be modified.

Leave out the final \n; otherwise you'll get a lot of blank lines.

Berend
Ed

Re: Find/Replace Regular Expression Anomaly

Post by Ed »

Hi Jerry

If you have a lot of code that needs "beautifying" you might want to consider freeware gc.exe from http://perso.club-internet.fr/cbeaudet
This does a fairly comprehensive job on C/C++. Most options can be turned off to leave you with only fixing brackets for instance.

It does take a bit of time to set up.

Ed
Jerry

Re: Find/Replace Regular Expression Anomaly

Post by Jerry »

Thanks Berend. Using $ instead of \n makes it work perfectly. I should have known that since I have written numerous Perl regular expressions and have used the $ many, many times.

Jerry
Post Reply