delete everything after the last semicolon on a line

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

delete everything after the last semicolon on a line

Postby sklad2 » Mon Jul 28, 2008 1:32 pm

I have lines like this

this is a test;delete all of this
test line two; this I want ; delete all of this
regular epxressions;are;great; when; you;know;how;to;use;them;delete this stuff


I want to delete after the last semicolon on each line.
I am using UE 14.10.1.0.1024
User avatar
sklad2
Advanced User
Advanced User
 
Posts: 58
Joined: Thu Mar 08, 2007 12:00 am

Re: delete everything after the last semicolon on a line

Postby mjcarman » Mon Jul 28, 2008 2:15 pm

Using Perl-style regular expressions:
Find What: [^;]*$
Replace With: ^p
User avatar
mjcarman
Power User
Power User
 
Posts: 123
Joined: Thu Feb 10, 2005 12:00 am

Re: delete everything after the last semicolon on a line

Postby sklad2 » Mon Jul 28, 2008 2:23 pm

This results in the following


this is a test;test line two; this I want ;regular epxressions;are;great; when; you;know;how;to;use;them;


Not what I was hoping for as a result. I want each line to just lop off the trailing stuff after the last semicolon.


looking more for this kind of result


this is a test;
test line two; this I want ;
regular epxressions;are;great; when; you;know;how;to;use;them;
User avatar
sklad2
Advanced User
Advanced User
 
Posts: 58
Joined: Thu Mar 08, 2007 12:00 am

Re: delete everything after the last semicolon on a line

Postby sklad2 » Mon Jul 28, 2008 9:26 pm

Fantastic!!!!!

Could you please explain the expression so I understand how it works.

Thanks a million
User avatar
sklad2
Advanced User
Advanced User
 
Posts: 58
Joined: Thu Mar 08, 2007 12:00 am

Re: delete everything after the last semicolon on a line

Postby Mofi » Tue Jul 29, 2008 2:42 am

I suggest a Perl regular expression search string [^;\r\n]+$ and an empty replace string. That would not change the current line ending format.

There is a help page about Perl regular expression in help of UltraEdit, but here is a short explanation.

Square brackets [...] means find any character specified inside the brackets.

If the first character after [ is ^ then the meaning is reversed. So [^...] means find any character except those specified inside the brackets.

\r is the character code for the character carriage return.
\n is the character code for the character line-feed.

So [^;\r\n] means find 1 character which is not a semicolon, not a carriage return and not a line-feed.

The + after the bracket means find the previous expression 1 or more times (* means 0 or more times). Because the previous expression means any character except semicolon, CR and LF, this regular expression selects now all strings not containing these 3 characters. But we want to find only those strings on end of line.

So the last regex character $, which means end of line without selecting the line ending characters, anchors the search to the end of the line.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: delete everything after the last semicolon on a line

Postby pietzcker » Tue Jul 29, 2008 5:07 am

Very good improvement, Mofi.

I would suggest yet another modification:

Search for
(?<=;)[^;\r\n]+$

and replace with nothing. The positive lookbehind expression (?<=;) makes sure that there is in fact a semicolon after which to delete the rest of the line. Otherwise, lines that don't contain a semicolon will be deleted entirely (save the CRLF).

If that's what is requested - but sklad2 wrote it that way :)
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm

Re: delete everything after the last semicolon on a line

Postby sklad2 » Wed Jul 30, 2008 7:36 am

It is working great!!!!

Code: Select all
(?<=;)[^;\r\n]+$

This is the one I am currently running.

I am thinking about getting Regex buddy. I hope this will help my regex skills :mrgreen:

Thanks for all of the great help with regular expression!!!!!!!! :D
User avatar
sklad2
Advanced User
Advanced User
 
Posts: 58
Joined: Thu Mar 08, 2007 12:00 am


Return to Find/Replace/Regular Expressions