This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Remove everything from last occurence

Help for those learning Tcl or writing their own scripts.
Post Reply
m
maphex
Voice
Posts: 9
Joined: Fri Feb 10, 2012 5:06 pm

Remove everything from last occurence

Post by maphex »

Trying my hand at some regex. I'm trying to take a string and remove everything after the last occurrence of and symbol.
I'm using

Code: Select all

regsub {\-.*} $text ""
but it only works if there is only one occurrence of "-" but if text has more than one "-" it removing everything from the first occurrence back. Any help?
I have tried to use string and trim right but that doesn't help either

Code: Select all

string range $text 0 [string last \- $text]-1
w
willyw
Revered One
Posts: 1205
Joined: Thu Jan 15, 2009 12:55 am

Re: Remove everything from last occurence

Post by willyw »

From the partyline in a bot of mine:

Code: Select all

[09:18] <mynick> .set text abc-def-ghi-jkl-mno-pqr-stu-vwx-yz
[09:18] <botnick> [14:18:30] #jacky# set text abc-def-ghi-jkl-mno-pqr-stu-vwx-yz
[09:18] <botnick> Ok, set.
[09:18] <mynick> .tcl regsub -start [string last \- $text] {\-.*} $text ""
[09:18] <botnick> Tcl: abc-def-ghi-jkl-mno-pqr-stu-vwx

I hope this helps.
For a fun (and popular) Trivia game, visit us at: irc.librairc.net #science-fiction . Over 300K Q & A to play in BogusTrivia !
m
maphex
Voice
Posts: 9
Joined: Fri Feb 10, 2012 5:06 pm

Post by maphex »

Will this remove "-" in addition to everything after it?
w
willyw
Revered One
Posts: 1205
Joined: Thu Jan 15, 2009 12:55 am

Post by willyw »

maphex wrote:Will this remove "-" in addition to everything after it?
??
That's copy-n-paste, in my post above. It shows you exactly what it does.

Better yet, experiment with it in a script on your own bot. :)

If it doesn't do what you wanted, then most likely it means that I didn't understand what you actually wanted.

Let us know how it goes.
For a fun (and popular) Trivia game, visit us at: irc.librairc.net #science-fiction . Over 300K Q & A to play in BogusTrivia !
w
willyw
Revered One
Posts: 1205
Joined: Thu Jan 15, 2009 12:55 am

Post by willyw »

maphex wrote:Will this remove "-" in addition to everything after it?
Are you familiar with using the
.tcl
command in the partyline?

If not, perhaps what I put in my post is not something you instantly recognize, and you need to look that up and get familiar with it. It is very handy, at times.
For a fun (and popular) Trivia game, visit us at: irc.librairc.net #science-fiction . Over 300K Q & A to play in BogusTrivia !
Post Reply