Find block of code delimited by ( ) with multiple lines

Find, replace, find in files, replace in files, regular expressions

Find block of code delimited by ( ) with multiple lines

Postby edd__ » Mon Jan 02, 2006 8:02 pm

This should be simple but I've spend the whole day without success.

Portion of code:
TABLESPACE reference
NOLOGGING
STORAGE ( INITIAL 10M
NEXT 10M
PCTINCREASE 0
)
BUILD IMMEDIATE
AS

All in the world I want to find is:
STORAGE ( INITIAL 10M
NEXT 10M
PCTINCREASE 0
)

Then I would replace it with nothing.

It would be simple if all was in a single line but multiple lines are a problem. Im not sure how to represent this because the clauses and number of lines between both parenthesis are variable. Just want to find the next ')' after the first 'STORAGE ('.

Thanks in advance!!
User avatar
edd__
Newbie
 
Posts: 5
Joined: Mon Jan 02, 2006 12:00 am

Re: Find block of code delimited by ( ) with multiple lines

Postby mrainey56 » Tue Jan 03, 2006 3:38 am

I did a search of the forum and came up with this thread: viewtopic.php?t=109

Studying it gave me the clues for this search string:

^.*[(].*\p*[)]

Unix style regex
User avatar
mrainey56
Master
Master
 
Posts: 212
Joined: Tue Jul 27, 2004 11:00 pm
Location: Spartanburg, South Carolina

Re: Find block of code delimited by ( ) with multiple lines

Postby edd__ » Tue Jan 03, 2006 11:11 am

mrainey56 wrote:^.*[(].*\p*[)]

Unix style regex

Hi! Thanks for your fast response, but it didn't work for me.

Also tried with ^.*[(](.*\p)*[)] without success.

Any other idea?
Forgetting my code I would reduce the problem to find an open '(' and then a closing ')' accross multiple lines.
User avatar
edd__
Newbie
 
Posts: 5
Joined: Mon Jan 02, 2006 12:00 am

Re: Find block of code delimited by ( ) with multiple lines

Postby mrainey56 » Tue Jan 03, 2006 11:29 am

It works fine for me, using your text sample. You have Unix mode set, and the Regular Expressions option checked?


What result do you get?
User avatar
mrainey56
Master
Master
 
Posts: 212
Joined: Tue Jul 27, 2004 11:00 pm
Location: Spartanburg, South Carolina

Re: Find block of code delimited by ( ) with multiple lines

Postby Mofi » Wed Jan 04, 2006 10:30 am

In UltraEdit style the regular expression for this is: ([~)]+).

Although this will work here, it is always very dangerous to use the [~]+ sequence because it is very aggressive. It can only be used in easy situations like your example.

So I have developed for myself a little macro to delete multi-line blocks.

This macro named DeleteBlocks deletes all blocks defined by 2 non regular expression strings which can be entered during macro execution from current file position to the end of the file. If someone wants it with regular expression insert the red marked commands.

Note: Regular expression searches with this method is only possible with UltraEdit style, not with Unix style (^c is not possible in Unix style).

Properties for macro DeleteBlocks:

Cancel Dialog = TRUE
Find with Replace = TRUE
Hot Key = what you prefer or NONE

Macro code:

InsertMode
ColumnModeOff
HexOff
UnixReOff
Key HOME
Key HOME
GetString "Enter first search string (start of block)"
StartSelect
Key HOME
Clipboard 9
Cut
EndSelect
GetString "Enter second search string (end of block)"
Clipboard 8
StartSelect
Key HOME
Cut
EndSelect
Loop
Clipboard 9
Find RegExp "^c"
IfNotFound
ExitLoop
EndIf
Clipboard 8
Find RegExp Select "^c"
IfSel
Delete
Else
ExitLoop
EndIf
EndLoop
Clipboard 9
ClearClipboard
Clipboard 8
ClearClipboard
Clipboard 0
UnixReOn

Remove the last red command, if you use regular expression in UltraEdit style by default instead of Unix style.
For UltraEdit v11.10c and lower see Advanced - Configuration - Find - Unix style Regular Expressions.
For UltraEdit v11.20 and higher see Advanced - Configuration - Searching - Unix style Regular Expressions.
Macro commands UnixReOn/UnixReOff modifies this setting.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4064
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Find block of code delimited by ( ) with multiple lines

Postby edd__ » Wed Jan 04, 2006 11:14 am

Hey guys thank you a lot, I took a bit of each and finally got a solution.

mrainey56, I've set Unix Syntax and RegExp but is still not working. Anyway you gave me the basics.
Mofi your ([~)]+) expression was very helpful too.

Here it is (at least this works for me):

STORAGE[ ]*([~(])([A-Z0-9 a-z\p]*)([~)])

Regards
ed
User avatar
edd__
Newbie
 
Posts: 5
Joined: Mon Jan 02, 2006 12:00 am

Matching across newlines

Postby nysus » Sun Sep 17, 2006 10:38 pm

Is there something equivalent to the "m" switch in Perl regular expressions that lets the "." character match a newline?

I'm trying to search and replace the first X number of lines in an HTML file up to a certain string with this:

<!DOCTYPE[.\n\r]*<!-- InstanceBeginEditable name="Main" -->

UltraEdit just reports it can't find the string, even though it does exist.
User avatar
nysus
Newbie
 
Posts: 4
Joined: Sun Sep 25, 2005 11:00 pm

Re: Matching across newlines

Postby Mofi » Mon Sep 18, 2006 10:37 am

nysus wrote:Is there something equivalent to the "m" switch in Perl regular expressions that lets the "." character match a newline?

I have merged your new thread with this existing thread. For an answer see above and adjust your search accordingly.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4064
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Matching across newlines

Postby nysus » Mon Sep 18, 2006 11:53 am

Well, I think the coders of UE need to rewrite their regex engine. While the suggestions above may work, they seem like hacks. Finding matches across lines could best be solved with a regex modifier like Perl.
User avatar
nysus
Newbie
 
Posts: 4
Joined: Sun Sep 25, 2005 11:00 pm


Return to Find/Replace/Regular Expressions