0

I have single letters and numbers in a variable that I would like to remove

example inputs:

USA-2019-1-aoiwer
USA-A-jowerasf
BB-a_owierlasdf-2019
flsfwer_5_2015-asfdlwer

desired outputs:

USA-2019--aoiwer
USA--jowerasf
BB-_owierlasdf-2019
flsfwer__2015-asfdlwer

my code:

bind pub "-|-" !aa proc:aa
proc proc:aa { nick host handle channel arg } {

    set line [lindex $arg 0]

    set line [string map {[a-z] """} $line]
    set line [string map {[0-9] """} $line]


    putnow "PRIVMSG $channel :$line"
}

Unfortunately that does not work and i have no other idea

Regards

fdelafuente
  • 1,114
  • 1
  • 13
  • 24
Hause Mica
  • 31
  • 4
  • Do you have some error message? – mkUltra May 28 '19 at 13:03
  • I'm not sure I understand how you want it to perform. How do you decide that in the first example, `1` should be removed, in the second, `A` should be removed, in the third, `a` should be removed and in the last, `5` should be removed? – Jerry May 28 '19 at 13:11
  • Jerry, t is always the single characters or numbers, only with length 1, regards – Hause Mica May 28 '19 at 13:26
  • See the regsub command. You will need to work out a regular expression that matches your desired data fields. For instance "[\_-][:alphanum:][\_-]" would match a single alphanumeric char with either underscore or hyphen on either side. – patthoyts May 28 '19 at 14:44
  • set line [regsub -all "[_-][:alphanum:][_-]" "" $line] error: invalid command name "_-" – Hause Mica May 28 '19 at 15:07

1 Answers1

1

string map would remove all the lowercase letters and numbers, if it worked. However, you also have unbalanced quotes, which causes a syntax error when the proc is resolving.

I would recommend using regsub. The hard part, however, would be to get a proper expression to do the task. I will suggest the following:

bind pub "-|-" !aa proc:aa

proc proc:aa { nick host handle channel arg } {
    set line [lindex $arg 0]
    regsub -nocase -all {([^a-z0-9]|\y)[a-z0-9]([^a-z0-9]|\y)} $line {\1\2} line
    putnow "PRIVMSG $channel :$line"
}

Basically ([^a-z0-9]|\y) matches a character that is non alphanumeric, or a word boundary (which will match at the beginning of a sentence for example if it can, or at the end of a sentence), and stores it (this is the purpose of the parens).

The matched groups are stored in order starting with 1, so in the replace portion of regsub, I'm placing the parts that shouldn't be replaced back where they were.

The above should work fine.


You could technically go a little fancier with a slightly different expression:

regsub -nocase -all {([^a-z0-9]|\y)[a-z0-9](?![a-z0-9])} $line {\1} line

Which uses a negative lookahead ((?! ... )).

Anyway, if you do want to get more in depth, I recommend reading the manual on regular expression syntax

Jerry
  • 70,495
  • 13
  • 100
  • 144