RosettaCodeData/Task/Regular-expressions/M2000-Interpreter/regular-expressions.m2000
2023-07-01 13:44:08 -04:00

73 lines
2.5 KiB
Text

Module CheckIt {
declare global ObjRegEx "VBscript.RegExp"
Function RegEx.Replace$(from$, what$) {
Method ObjRegEx, "Replace", from$, what$ as response$
=response$
}
Function RegEx.Test(what$) {
Method ObjRegEx, "Test", what$ as response
=response
}
Print Type$(ObjRegEx)
With ObjRegEx, "Global", True, "Pattern" as pattern$
pattern$="Mona Lisa"
Print RegEx.Test("The Mona Lisa is in the Louvre.")=true
Print RegEx.Replace$("The Mona Lisa is in the Louvre.", "La Gioconda")
Pattern$ = " {2,}"
Print "Myer Ken, Vice President, Sales and Services"
\\ Removing some spaces
Print RegEx.Replace$("Myer Ken, Vice President, Sales and Services", " ")
pattern$="(\d{3})-(\d{3})-(\d{4})"
Method ObjRegEx, "Execute", "555-123-4567, 555-943-6717" as MyMatches
Print Type$(MyMatches) ' it is a IMatchCollection2
With MyMatches, "Count" as count, "Item" as List$()
For i=0 to Count-1 : Print List$(i) : Next i
Print RegEx.Replace$("555-123-4567, 555-943-6717", "($1) $2-$3")
Pattern$ = "(\S+), (\S+)"
Print RegEx.Replace$("Myer, Ken", "$2 $1")
Method ObjRegEx, "Execute", "Myer, Ken" as MyMatches
Rem : DisplayFunctions(MyMatches)
\\ we can use Enumerator
With MyMatches, "_NewEnum" as New Matches
Rem : DisplayFunctions(Matches)
With Matches, "Value" as New item$
While Matches {
Print Item$
}
\\ Or just using the list$()
For i=0 to Count-1 : Print List$(i) : Next i
declare ObjRegEx Nothing
End
Sub DisplayFunctions(x)
Local cc=param(x), ec=each(cc)
while ec {
Print eval$(ec) ' print every function/property of object x
}
End Sub
}
Checkit
\\ internal has no pattern. There is a like operator (~) for strings which use pattern matching (using VB6 like). We can use Instr() and RInstr() for strings.
Module Internal {
what$="Mona Lisa"
Document a$="The Mona Lisa is in the Louvre."
Find a$, what$
Read FindWhere
If FindWhere<>0 then Read parNo, parlocation
\\ replace in place
Insert FindWhere, Len(what$) a$="La Gioconda"
Report a$
n$="The Mona Lisa is in the Louvre, not the Mona Lisa"
Report Replace$("Mona Lisa", "La Gioconda", n$, 1, 1) ' replace from start only one
dim a$()
a$()=Piece$("Myer, Ken",", ")
Print a$(1)+", "+a$(0)="Ken, Myer"
}
Internal