Search and replace over multiple lines?

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

Re: Search and replace over multiple lines?

Postby Purposeinc » Sun Oct 01, 2006 12:43 am

I tried it and I think it worked. Let me know if this sounds correct.

==============================
Select the text you want to use as your find.
Click on Copy Selection to Clipboard button.
Select the text you want to paste.

Click on SEARCH
Click on REPLACE IN FILES
Enter ^c in the find box.
Enter ^s in the replace box.
==============================

It appeared to work. I am going to test it more before using it for real

Please comment if I left anything out.

Thank you again!
dk
User avatar
Purposeinc
Newbie
 
Posts: 4
Joined: Tue Sep 26, 2006 11:00 pm

Re: Search and replace over multiple lines?

Postby Mofi » Sun Oct 01, 2006 8:36 am

You have perfectly understand how to do the block replaces. Take care with the format of your HTML files. If all are DOS files, no problem. If all are UNIX files, disable the configuration option Automatically convert to DOS format before loading 1 HTML file and executing the procedure you posted.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Search and replace over multiple lines?

Postby BLenhard » Wed Jan 31, 2007 4:01 am

Hello, I am trying to search/replace (in files) over multiple lines where I don't know for sure how many lines there are. How can I do this when none of the wildcard characters apply to newline? Can anyone help me with the syntax?
Code: Select all
<script>
....
....
....
</script>


Thank You,

Bill
User avatar
BLenhard
Newbie
 
Posts: 1
Joined: Tue Jan 30, 2007 12:00 am

Re: Search and replace over multiple lines?

Postby Mofi » Thu Feb 01, 2007 3:24 pm

Try <script>[~<]+</script> (UltraEdit style)!

This works only if there is no < inside the script block. And make backups of all your files because usage of [~<]+ is very dangerous here. Better would be to find/use a more complex regular expression which contains as much fixed text as possible to avoid replacing too much.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Search and replace over multiple lines?

Postby tengiz » Thu Feb 15, 2007 8:15 am

I have similar requirement where my text looks like

/************
...
...
...
...
************/

I need to search and replace multiple lines starting with /* and ending with */. The challenge is that '*' is a regular expression symbol in UE.

Thanks in advance,
tengiz
User avatar
tengiz
Newbie
 
Posts: 1
Joined: Wed Feb 14, 2007 12:00 am

Re: Search and replace over multiple lines?

Postby Mofi » Thu Feb 15, 2007 8:56 am

No problem, escape the * character. For UltraEdit style you can use for example following search string:

%[ ^t]++/^*^*^*+[~*]+^*^*^*+/[ ^t]++^p

%[ ^t]++ means from start of a line with 0 or more spaces or tabs.

/^*^*^*+ followed by /*** and more *.

[~*]+ all characters except *. That means it will not work if there is a block comment which contains a * inside the comment.

^*^*^*+/ followed by *** and more * before character /.

[ ^t]++^p matches possible existing trailing spaces and tabs and the DOS line termination.

But the better and more secure method to delete blocks with a defined start and end string, but undefined text between is to use a macro with the find select feature. Example macro which needs macro property Continue if a Find with Replace not found checked.

InsertMode
ColumnModeOff
HexOff
Top
Loop
Find "/***"
IfNotFound
ExitLoop
EndIf
Key HOME
IfColNumGt 1
Key HOME
EndIf
StartSelect
Find Select "***/"
IfSel
Delete
EndSelect
DeleteLine
Else
EndSelect
ExitLoop
EndIf
EndLoop
Top

The macro above contains some extra code for security. For your example the following simplified macro would do also the job:

InsertMode
ColumnModeOff
HexOff
Top
Loop
Find "/***"
IfNotFound
ExitLoop
EndIf
StartSelect
Find Select "***/"
Delete
EndSelect
DeleteLine
EndLoop
Top
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Search and replace over multiple lines?

Postby golbez » Fri Feb 16, 2007 10:12 pm

I have a question with this.

Note: I am very new to the program and it would just be good to say I'm not completely comprehending what is being said in this post.

Using HTML

With that being said my question involves replacing a multi-line of text between <style> </style> with a space in effectively of removing style and all of it's contents. (for example)

<style>
jhjkhkhjkhjkhjk
hjkhjkhjkhjk
hhjkhjkhjk
</style>

I want to delete style and everything in it. This is some old code we are trying to remove from quite a few files in our directory. We would just copy all of the code from <style> to </style> seperated by the classic ^p but the code varies in each instance of the multi-lines of text.

Thank You
User avatar
golbez
Newbie
 
Posts: 1
Joined: Mon Jan 29, 2007 12:00 am

Re: Search and replace over multiple lines?

Postby Mofi » Sat Feb 17, 2007 4:52 pm

Hi golbez!

And what is your question?

<script>...</script>, /***...***/ or <style>...</style> that makes no difference. The methods - regular expression replace or macro - are already explained above. In your case just the start and end strings are different.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Previous

Return to Find/Replace/Regular Expressions

cron