| Message |
I got this tip during the MUD Meet and Greet. You can do your own regular expression parsing inside a script (for example, you might want to match on more than 9 wildcards).
From what I can see of the syntax, the regular expressions are very similar to the ones used by MUSHclient internally.
Below is an example - you can try that in an "immediate" window.
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches ' Create variable.
Set regEx = New RegExp ' Create a regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True ' Set case insensitivity.
regEx.Global = True ' Set global applicability.
Set Matches = regEx.Execute(strng) ' Execute search.
For Each Match in Matches ' Iterate Matches collection.
RetStr = RetStr & "Match found at position "
RetStr = RetStr & Match.FirstIndex & ". Match Value is '"
RetStr = RetStr & Match.Value & "'." & vbCRLF
Next
RegExpTest = RetStr
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))
The person I was talking to was using them inside a trigger, taking the actual trigger text to re-evaluate the regular expression inside the trigger, like this ...
sub MyTrigger (sName, sLine, wildcards)
Set regEx = New RegExp
' get trigger match text
regEx.Pattern = world.GetTriggerInfo(sName, 1)
Set Matches = regEx.Execute(sLine)
For Each match in Matches
'
' do something here
'
Next
end sub
There are also "Test" and "Replace" methods.
Test just tests to see if the regular expression matched or not.
Replace replaces one with another.
For example, here is how you can replace one string with another ...
Function ReplaceTest(patrn, replStr)
Dim regEx, str1 ' Create variables.
str1 = "The quick brown fox jumped over the lazy dog."
Set regEx = New RegExp ' Create regular expression.
regEx.Pattern = patrn ' Set pattern.
regEx.IgnoreCase = True ' Make case insensitive.
ReplaceTest = regEx.Replace(str1, replStr) ' Make replacement.
End Function
MsgBox(ReplaceTest("fox", "cat")) ' Replace 'fox' with 'cat'.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|