Jump to content

VBScript help: find/replace?


Gloomlove

Recommended Posts

I'm not new to VBScript, just very bad at it. Just a warning in case, y'know, extreme familiarity is expected.The script in question is for the behavior of a custom-made Battle.net chatbot. As an example of what I want to do, I'll put in one part of a Select Case.

			   Case Else					Message = Replace(Message, LCase(MyCurUsername), Pnk & MyCurUsername & "µ096096096")					TSColor "Nrm"					ChatAdd TSCTxt & "<From: " & Account & "> µ096096096" & Message		  End Select

The line with the replace is what I'm having issues with. If I do

Message = Replace(Message, LCase(MyCurUsername), Pnk & MyCurUsername & "µ096096096")

it doesn't always catch my username due to people capitalizing it differently. And if I do

Message = Replace(LCase(Message), LCase(MyCurUsername), Pnk & MyCurUsername & "µ096096096")

EVERYTHING comes out lowercase, even though it catches my name. What I want is to have it catch my name however it is capitalized and replace it with the

Pnk & MyCurUsername & "µ096096096"

bit without trashing the sentence's capitalization.Help much appreciated, thanks! ^^

Link to comment
Share on other sites

I'm not entirely clear on where I should set the regular expression. Should it be at the top or just in the sub where I'm using it?Thanks for the fast response and the link by the way ^^EDIT: Also, the site is unclear on how you use the replace function. I assumed it would be something like

UNHRegExp.Replace(Message,  Pnk & MyCurUsername & "µ096096096")

or

Message = UNHRegExp.Replace(Message,  Pnk & MyCurUsername & "µ096096096")

but those don't seem to work.Help appreciated ^^EDIT: I figured it out but have NO CLUE how to fix it.It's doing it just fine, it's just that my username contains square brackets ([, ]). I can't escape them with backslashes because it's contained in a string called MyCurUsername -- which is also used for connecting to Battle.net. And even if it WEREN'T, I have no idea how to replace square brackets with escaped square brackets (So I can't just Dim a new string and be done with it, unfortunately.) Suggestions? xx'

Link to comment
Share on other sites

Forgive my VBScript, it's been many, many years.... Also, I can't test this, but I think this is what you're looking for:

Set reg = New RegExpreg.IgnoreCase = Truereg.Global = Truereg.Pattern = MyCurUsernameMessage = reg.Replace(Message, Pnk & MyCurUsername & "µ096096096")

Link to comment
Share on other sites

As I said above in my last-minute edit, that won't work because of the square brackets. xx'What I really need is something that'll escape MyCurUsername (or another string's) illegal characters...including dots, parentheses, etc. Obviously, that's beyond my abilities.EDIT: If anyone is willing to help me make such a thing though... >> I'd be infinitely grateful!

Link to comment
Share on other sites

Hate to double post, but I think I have a breakthrough.. that still needs work.I added a new sub:

Sub UserNameCharEsc'------------'Dim settings'------------	 Set UNERegExp = New RegExp	 UNERegExp.IgnoreCase = True	 UNERegExp.Global = True	 UNERegExp.Pattern = "["'	 EscUsername = UNERegExp.Replace(MyCurUsername, "\[")	 Set UNERegExp.Pattern = "]"	 EscUsername = UNERegExp.Replace(EscUsername, "\]")	 Set UNERegExp.Pattern = "_"	 EscUsername = UNERegExp.Replace(EscUsername, "\_")	 Set UNERegExp.Pattern = "("	 EscUsername = UNERegExp.Replace(EscUsername, "\(")	 Set UNERegExp.Pattern = ")"	 EscUsername = UNERegExp.Replace(EscUsername, "\)")	 Set UNERegExp.Pattern = "-"	 EscUsername = UNERegExp.Replace(EscUsername, "\-")	 Set UNERegExp.Pattern = "~"	 EscUsername = UNERegExp.Replace(EscUsername, "\~")	 Set UNERegExp.Pattern = "@"	 EscUsername = UNERegExp.Replace(EscUsername, "\@")End Sub

And then I edited the UNHRegExp pattern used in the actual AddText sub to use the string EscUsername.Output?[12:09:58.423] ~Dirge_ Dirge_DDirge_iDirge_rDirge_gDirge_eDirge__Dirge_~Obviously I messed up somewhere. This is the relevant part of the test sub I used:

			   Case LCase(MyCurUsername)					Message = UNHRegExp.Replace(Message, Grn & MyCurUsername & Pnk)					TSColor "Mne"					ChatAdd TSCTxt & "~" & Account & " " & Message & "~"

Help appreciated ^^EDIT: Idea occurred. Should I perhaps use an If statement or Select Case?

Link to comment
Share on other sites

What is the original input? "~Dirge_" ? Also, what is the list of valid characters that can make up a username?In your escaping algorithm, the only characters that you'd truly need to escape would be [, ], (, and ). The underscore (_), the tilde (~), and the at (@) have no special meaning, so they don't need to be escaped. The hypen (-) only has special meaning when it is within square brackets like so: [a-z]. If you wanted to match a through z and hypen, the expression could look like this: [-a-z] or this [a-z\-]. But in the case of your username, I don't think it needs to be escaped.

Link to comment
Share on other sites

It wasn't working previously when I had tested. So I just shoved in everything I could. xx'The original input (if you'll look at the part of the Select Case at the bottom of my last post) was Dirge_ for account and Dirge_ for message.And I just tested again without escaping the things you deemed unnecessary. Same output, exactly.EDIT: What I mean by "same output" is it's still giving that glitchy repitition of the highlighted name and the spaced out original.EDIT: Oh, and valid usernames for LOGGING ON can contain (, ), [, ], {, }, ^, !, @, #, $, ', -, _, ~, `, . and alphanumeric characters. For actually making accounts it's limited to -, _, (, ), [, ], . and alphanumeric characters, and depending on the username you might be unable to use (, ), [, or ].EDIT: I think I figured out the problem. ... It's BAD, and I don't think it's supposed to work this way.Input: Dirge_ for Account, and Test for Message.Output?[13:02:36.533] ~Dirge_ Dirge_TDirge_eDirge_sDirge_tDirge_~Insight? xx'EDIT: Just realized that I never mentioned changing accounts for testing purposes. My previous username was x[Gloom]x, switched to Dirge_ because of the brackets.

Link to comment
Share on other sites

Insight? xx'
Well, it looks like "Account" is being written correctly. So that leaves "Message" which gets output as:Dirge_TDirge_eDirge_sDirge_tDirge_If Message was "Test" before running it through the replace, it appears that the replace function is treating "Test" as "[Test]" and replacing "T" with "Dirge_T", "e" with "Dirge_e" and so on.At this point in your code:
Case LCase(MyCurUsername)					Message = UNHRegExp.Replace(Message, Grn & MyCurUsername & Pnk)					TSColor "Mne"					ChatAdd TSCTxt & "~" & Account & " " & Message & "~"

What is the pattern for UNHRegExp?

Link to comment
Share on other sites

EscUsername, which should be Dirge_.EDIT:

Sub UserNameCharEsc'------------'Dim settings'------------	 Set UNERegExp = New RegExp	 UNERegExp.IgnoreCase = True	 UNERegExp.Global = True	 UNERegExp.Pattern = "["'	 EscUsername = UNERegExp.Replace(MyCurUsername, "\[")	 Set UNERegExp.Pattern = "]"	 EscUsername = UNERegExp.Replace(EscUsername, "\]")	 Set UNERegExp.Pattern = "("	 EscUsername = UNERegExp.Replace(EscUsername, "\(")	 Set UNERegExp.Pattern = ")"	 EscUsername = UNERegExp.Replace(EscUsername, "\)")End Sub

Sub AddText (Account, Message, Style)'----'Dims'----Dim UTStDim WhStDim EmStDim JnStDim LvSt''------------'Dim settings'------------	 Set UNHRegExp = New RegExp	 UNHRegExp.IgnoreCase = True	 UNHRegExp.Global = True	 UNHRegExp.Pattern = EscUsername	 JnSt = " has joined channel " & MyChannel & "! ***"	 LvSt = " has left the channel. ***"

And MyCurUsername is Dirge_EDIT: Could it be that the UserNameCharEsc sub is adding the brackets?EDIT: Problem solved... RegExp weren't needed. I had to add , 1, -1, 1 to the end of a normal Replace.Thanks anyway^^

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...