3/31/2010

LS2J: Match string with regular expression




Have you ever tries LS2J in your LotusScript?
This is the way to connect to Java class from LotusScript

Some of you may not have lots of time to learn new technology, but there're only 9 classes for LS2J.

As an example, I invoked java.util.regex.Pattern class and java.util.regex.Matcher class for use of regular expression and this sample code shows simple regular expression string match. some people may be missing regular expression of Java or perl.. With LS2J, you can use it!

See also following site to check what expressions can be used
Pattern class (Java SDK document)

' Module for LS2J Uselsx "*javacon" Sub Click(Source As Button) Dim b As Boolean teststr$ = "あああ" '"do not contain ASCII char = All of strings are DBCS" regex$ = "[^\p{ASCII}]*" replacedstr$ = "Test" b = MatchString(teststr$ ,regex$) Msgbox "Input: " & teststr$ & Chr(13) &; "Pattern string: " & regex$ & Chr(13) & "Result: " & Cstr(b) End Sub Function MatchString( InputStr As String, RegEx As String) As Boolean ' InputStr: Input string to test ' RegEx: Regular expression formula for match ' Return Value: True if matched Dim j As JavaSession Dim c As JavaClass Dim pattern As JavaObject Dim matcher As JavaObject Dim e As JavaError Set j = New JavaSession Set c = j.GetClass("java.util.regex.Pattern") Set pattern = c.compile(RegEx) Set matcher = pattern.matcher(InputStr) MatchString = matcher.matches() End Function

2 comments:

  1. Many thanks, Hirotaka-san. This is very nice code and just the sort of thing I like to have handy in a script library!

    ReplyDelete
    Replies
    1. Thanks Carter
      Glad to hear that this simple code is included in your library.

      Delete