replace all text that follows something

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

replace all text that follows something

Postby user » Thu Sep 14, 2006 5:10 am

hello

I want to scan each line (not sentence or anything else) and delete (replace with nothing) all visible characters (eg numbers, symbols, letters, but not paragraph ends) that follow (that is at the right of) the first occurance in line of this: ',something' (without quotemarks)

thanks
User avatar
user
Newbie
 
Posts: 4
Joined: Thu Jun 08, 2006 11:00 pm

Re: replace all text that follows something

Postby Bego » Thu Sep 14, 2006 7:00 am

Hm, an example was nice, but if I understood it right, a macro like this should do the job:

Code: Select all
InsertMode
ColumnModeOff
HexOff
UnixReOff
Loop
Find ",something"
IfFound
Key RIGHT ARROW
Key LEFT ARROW
StartSelect
Key END
EndSelect
Key DEL
Else
ExitLoop
EndIf
EndLoop


Macro property "repeat if nothing found" has to be set.

Before:
bla ,something fghg hgfh
bla something without something else
bla ,something and something else
bla ,something and something else
bla something without something else
bla ,something and something else


Afterwards
bla ,something
bla something without something else
bla ,something
bla ,something
bla something without something else
bla ,something


rds Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: replace all text that follows something

Postby user » Thu Sep 14, 2006 7:09 am

thanks

I saved the above code in a text file but I cant make it work as macro (not valid macro)
User avatar
user
Newbie
 
Posts: 4
Joined: Thu Jun 08, 2006 11:00 pm

Re: replace all text that follows something

Postby Bego » Thu Sep 14, 2006 8:15 am

Why text file?
Copy the statements above with ctrl-c, goto Ultraedit, open say "New Macro" in the macro menu.
Now just give the macro a name, set the property as described above and paste the code (ctrl-v) there.
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: replace all text that follows something

Postby user » Thu Sep 14, 2006 8:47 am

thanks

can you tell me please the opposite? I mean to delete previous things

thanks
User avatar
user
Newbie
 
Posts: 4
Joined: Thu Jun 08, 2006 11:00 pm

Re: replace all text that follows something

Postby Bego » Thu Sep 14, 2006 9:56 am

quite similar:

InsertMode
ColumnModeOff
HexOff
UnixReOff
Loop
Find ",something"
IfFound
Key Ctrl+LEFT ARROW
StartSelect
Key HOME
EndSelect
Key DEL
Else
ExitLoop
EndIf
EndLoop


But in your specific example, you have to place a
"KEY LEFT ARROW"
after "Key Ctrl+LEFT ARROW"
because comma and something are "2 words"

Also "Key HOME" is dependant of your UE setting, if KEY HOME is always pointing to column 1 or not. Normally, it should work.
If not, search for "KEY HOME" macro in this forum.

rds Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Re: replace all text that follows something

Postby Mofi » Thu Sep 14, 2006 11:13 am

Hi Bego!

Why using macros for this. A single regular expression replace ALL can do the same.

Delete everthing after ',something' in UltraEdit style:

Find: ,something*$
Replace: ,something


Delete everthing before ',something' in UltraEdit style:

Find: %*,something
Replace: ,something


Delete everthing after ',something' in Unix/Perl style:

Find: ,something.*$
Replace: ,something


Delete everthing before ',something' in Unix/Perl style:

Find: ^.*,something
Replace: ,something
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: replace all text that follows something

Postby Bego » Thu Sep 14, 2006 11:41 am

Hi Mofi, you're right!
But why the easy way if there is a complicated solution ? ;-)
I think I was in a macro blindness ;-)
...or a little regexp angry cause perl regexps work so badly.

rds Bego
User avatar
Bego
Master
Master
 
Posts: 357
Joined: Wed Nov 24, 2004 12:00 am
Location: Germany

Delete ALL characters before or after a string

Postby jer456 » Tue Sep 04, 2007 5:43 am

I'm using UltraEdit 13.10a+1, I am using the UE style for regexes.

Question:

How do I delete ALL characters - inlcuding line breaks and invisible characters - before a given string? And how do I delete ALL characters - including line breaks and invisibles - after a given string?

For example:

I want to delete all the characters before the HTML tag:
<div class="tablejdb"> in my webpage. THEN I want to delete all the characters after the <div class="text"> HTML tag in my webpage.

Yes, I already read Mofi's solution to a similar problem, but this does not work for me because it does not delete line breaks and invisible characters preceding or succeeding the string.
User avatar
jer456
Newbie
 
Posts: 6
Joined: Sun Sep 02, 2007 11:00 pm

Re: Delete ALL characters before or after a string

Postby Mofi » Tue Sep 04, 2007 11:54 am

See my macro at Run Macro on all files within folder. You can also use FindInFiles option Recursive if the files are spread over multiple directories.

Or open all files and use my macro at following macro.

Loop
IfNameIs ""
ExitLoop
EndIf
Top
StartSelect
Find MatchCase Select "<div class="tablejdb">"
IfSel
Key Ctrl+LEFT ARROW
Key Ctrl+LEFT ARROW
Key Ctrl+LEFT ARROW
Key Ctrl+LEFT ARROW
Key Ctrl+LEFT ARROW
Key LEFT ARROW
Delete
EndIf
Find MatchCase "<div class="text">"
IfFound
EndSelect
Key LEFT ARROW
Key RIGHT ARROW
SelectToBottom
Delete
CloseFile Save
EndLoop

You need a macro for that job because the simple regular expression replaces I posted are only for deleting everything before and after a string within a line only. You want delete blocks.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Delete ALL characters before or after a string

Postby jer456 » Tue Sep 04, 2007 2:06 pm

Thanks, it worked, im happy. I've learned a lot today
User avatar
jer456
Newbie
 
Posts: 6
Joined: Sun Sep 02, 2007 11:00 pm

Re: Delete ALL characters before or after a string

Postby pietzcker » Wed Sep 05, 2007 6:14 am

You could use a regular expression instead of a macro in a "replace in files" command, too. Since the dot (.) doesn't match line breaks in UE, and there's no way I know of to change that behaviour, I needed to use a kludge to make it work. Probably there's a more elegant way to do it.

What you need is a character that you know will NOT occur in your file. Say a character like ÿ (ASCII 255).

Then you can search for
Code: Select all
[^ÿ]*(<div class="tabledb">[^ÿ]*?<div class="text">)[^ÿ]*
and replace with
Code: Select all
\1

Enable Perl style regular expressions for this search.

HTH,
Tim
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: Delete ALL characters before or after a string

Postby Mofi » Thu Sep 20, 2007 1:42 pm

I know the method suggested by Tim and I have also posted it somewhere. But this one is dangerous.

In the past using this method to search for something which does not exist until a specified string is found produced not the result I (we) would expect. I don't know if this still happens, but in previous versions of UltraEdit with the UltraEdit regex engine the [~ÿ]++ regular expression selected everything to last occurence of the next specified string which is here <div class="tabledb">. I would have expect that [~ÿ]++ stops on first occurence of <div class="tabledb"> and not on last, but that was not the case.

Second be careful with tagged expressions. I don't know if there is a maximum limit for the number of bytes of a found tagged string. If UE/UES works with static arrays for the found tagged strings a regular expression like (<div class="tabledb">[^ÿ]*?<div class="text">) could result in a loss of needed data when the number of bytes is to large. That's very dangerous especially in a Replace In Files operation which cannot be undone.

That's the reason why I use for block deletions the Find Select "..." method. It's safe.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Find/Replace/Regular Expressions