MapInfo Pro Developers User Group

Expand all | Collapse all

Will anyone upgrade Mapbasic Regex to work in 64-bit

  • 1.  Will anyone upgrade Mapbasic Regex to work in 64-bit

    Posted 08-24-2018 00:14

    Source code is in https://nyalldawson.net/2013/05/regular-expressions-in-mapbasic/

    This would be a very useful ability in Mapbasic...



  • 2.  RE: Will anyone upgrade Mapbasic Regex to work in 64-bit

    Posted 08-27-2018 09:12

    Hi George

    What part of Nyall's tool have you been using? And are you using these from a MapBasic application only?

    One of our engineers did a sample of a new tool that can be done when we release MapInfo Pro 17.0.1 within the next week or so.

    He basically wrapped a .NET RegEx method into a MapBasic function and published this Mapbasic function for use within MapInfo Pro.

    This is a new feature coming in MapInfo Pro - being able to publish MapBasic custom functions to the MapInfo Pro interface.



  • 3.  RE: Will anyone upgrade Mapbasic Regex to work in 64-bit

    Posted 08-28-2018 00:21
      |   view attached

    Hi,

    I've been using the RegExMatch function in code like below...

    Declare Function RegExMatch Lib "C:\Program Files (x86)\MapInfo_v15\MapBasic\MBRegEx.dll" Alias "regex_match" (ByVal strInput as String, ByVal strMatch as String) As String

     

    close all

     

    'Adding Building Plans

     

    'Run program "Cmd.exe /c dir G:\Projects\108_SurveyPlanLinking\Input\BuildingPlans\*.* /b /w > G:\Projects\076_Monthly_DCDB_Update\Working\current\scratch\Building_List.txt"

    'Print "Created list of Building Plans"

    Register Table "G:\Projects\076_Monthly_DCDB_Update\Working\current\scratch\Building_List.txt" TYPE ASCII Delimiter 44 Titles Charset "WindowsLatin1" Into "G:\Projects\076_Monthly_DCDB_Update\Working\current\scratch\~MAP0001.TAB"

    Open Table "G:\Projects\076_Monthly_DCDB_Update\Working\current\scratch\~MAP0001.TAB" Hide

    Commit Table ~MAP0001 As "G:\Projects\076_Monthly_DCDB_Update\Working\current\scratch\Building_List.TAB"

    Close Table ~MAP0001

    Open Table "G:\Projects\076_Monthly_DCDB_Update\Working\current\scratch\Building_List.TAB"

    Alter Table "Building_List" ( add PCL Char(9), Address Char(100) ) Interactive

    Browse * From Building_List

    Update Building_List Set PCL = RegExMatch(Building,"^\d+-v1-\d+\s{2,}(\d+).*\s{1,2}.*$")

    Update Building_List Set Address = RegExMatch(Building,"(([a-zA-z]{3}\s+)?\d+\s+([a-zA-z]+\s+)*[a-zA-z]+)\..*$")

    stop

    ==========

    This results in the following

    Capture



  • 4.  RE: Will anyone upgrade Mapbasic Regex to work in 64-bit

    Posted 10-12-2018 03:31
      |   view attached

    Hi George

    I have given this a try using the .NET implementation of RegEx.

    I created three RegEx functions and implemented three MapBasic declarations for these. I can now use these functions like this:

    Dim sInput, sPattern As String

    sInput = "1486215-v1-509133 659698 106222 106223 11 Bamboo Street.TIF"

    sPattern = "(([a-zA-z]{3}\s+)?\d+\s+([a-zA-z]+\s+)*[a-zA-z]+)\..*$"

    Print "Found: " & RegExIsMatch (sInput, sPattern)

    Print RegExReplace(sInput, sPattern, "[Address found here]")

    Print "Address: " & RegExFirstMatch(sInput, sPattern)

    sPattern = "^\d+-v1-\d+\s{2,}(\d+).*\s{1,2}.*$"

    Print "Found: " & RegExIsMatch (sInput, sPattern)

    Print RegExReplace(sInput, sPattern, "[PCL found here]")

    Print "PCL: " & RegExFirstMatch(sInput, sPattern)

     

    I do however not get quite the same results as you do, not sure if that's caused by the .NET RegEx or the inputs that I'm using. Maybe there are more spaces in the Building string than I can decifre from your image.

    You should also be able to use the functions in a Update statement as you describe above. Use the RegExFirstMatch method to replace your current RegExMatch function.

    Maybe you can give it a try and see how it works on your end.

    I have attached the .NET assembly that you need to place in the same folder as your MBX and a file with the declarations for the .NET methods in the assembly.

    Let me know how it goes

    Attachment(s)



  • 5.  RE: Will anyone upgrade Mapbasic Regex to work in 64-bit

    Posted 10-12-2018 03:34

    PS: The .NET Methods have been implemented like this:

        //<summary>RegEx function that does a search and replace

        /// </summary>

        /// <param name="input">Input string to search</param>

        /// <param name="pattern">Pattern to look for in input string</param>

        /// <param name="replace">String to insert as replacement for pattern</param>

        /// <returns>string: input string where the pattern has been replaced with the replace string</returns>

        public static string RegExReplace(string input, string pattern, string replace)

        {

          return Regex.Replace(input, pattern, replace);

        }

     

        //<summary>RegEx function that looks for a pattern in a string

        /// </summary>

        /// <param name="input">Input string to search</param>

        /// <param name="pattern">Pattern to look for in input string</param>

        /// <returns>bool: returns true if the pattern is found in the input string</returns>

        public static bool RegExIsMatch(string input, string pattern)

        {

          return Regex.IsMatch(input, pattern);

        }

     

        //<summary>RegEx function that looks for a pattern in a string and returns the found pattern

        /// </summary>

        /// <param name="input">Input string to search</param>

        /// <param name="pattern">Pattern to look for in input string</param>

        /// <returns>string: returns the first found pattern in the input string</returns>

        public static string RegExFirstMatch(string input, string pattern)

        {

          Match m = Regex.Match(input, pattern);

          if (m.Success)

            return m.Value;

          else

            return "";

        }



  • 6.  RE: Will anyone upgrade Mapbasic Regex to work in 64-bit

    Posted 10-15-2018 00:23
      |   view attached

    Thanks for doing this - I am not familiar enough with .net to fully understand how to implement this.

     

    I tried adding the following to the mb

    Declare Function RegExMatch Lib "D:\Scratch\WindowHelper.dll" Alias "RegExIsMatch" (ByVal sInput as String, ByVal sPattern as String) As String

     

    But I get

    Capture

     

    Code is

    Declare Function RegExIsMatch Lib "D:\Scratch\MIRegex\WindowHelper.dll" Alias "RegExIsMatch" (ByVal sInput as String, ByVal sPattern as String) As String

     

    Dim sInput, sPattern As String

     

    sInput = "1486215-v1-509133 659698 106222 106223 11 Bamboo Street.TIF"

     

    sPattern = "(([a-zA-z]{3}\s+)?\d+\s+([a-zA-z]+\s+)*[a-zA-z]+)\..*$"

     

    Print "Found: " & RegExIsMatch (sInput, sPattern)



  • 7.  RE: Will anyone upgrade Mapbasic Regex to work in 64-bit

    Posted 10-15-2018 16:32

    Ah, sorry. Imight have forgotten to include the declare you should use.

    Try to use these declares, as you can see I have implemented a few RegEx functions:

    Declare Method RegExReplace Class "WindowHelper.Controller" Lib "WindowHelper.dll" (ByVal sInput As String, ByVal sPattern As String, ByVal sReplace As String) As String

    Declare Method RegExIsMatch Class "WindowHelper.Controller" Lib "WindowHelper.dll" (ByVal sInput As String, ByVal sPattern As String) As Logical

    Declare Method RegExFirstMatch Class "WindowHelper.Controller" Lib "WindowHelper.dll" (ByVal sInput As String, ByVal sPattern As String) As String

     



  • 8.  RE: Will anyone upgrade Mapbasic Regex to work in 64-bit

    Posted 10-15-2018 18:25
      |   view attached

    That works now but I seem to be getting different results when running the same regex in 32bit and 64 bit on the same data...what am I missing here?

    Capture

    Left is 64bit and right is 32 bit

    I tried RegExIsMatch and RegExFirstMatch in both Address s including the .TIF and in PCL it is returning other numbers.

     

    The code used is in https://drive.google.com/file/d/1Q9MzQD_e41JopCVHBogJ7PNvmDR8krLQ/view?usp=sharing

     

    One (last) question -is it possible to call this from within a mapinfo 64bit, mapbasic window so we can run and test regex statements without having to run the whole mb program?



  • 9.  RE: Will anyone upgrade Mapbasic Regex to work in 64-bit

    Posted 10-29-2018 09:37

    Hi George

    I'm really a newbie when it comes to Regular Expressions so I might be wrong here but I think my implementation is working correctly.

    You are asking for it to match a certain expression including "-v1-" and this string is returned. The column you insert it into seems to be too short to fit the entire string returned.

    If you use this pattern instead, you will get the first numeric value with a space before and after the numeric value:

    sPattern = "\s{1}\d+\s{1}"

    Is that what you are looking for?

    I'm working on a new version of WindowHelper that if run in MapInfo Pro 17.0.1 will publish a number of functions to the MapInfo Pro interface. This will mean that you can use the custom functions from WindowHelper from the SQL Select dialog, the Update Column dialog, Label Expressions and you can also use the functions directly from the MapBasic window too.

    I have published it yet on Community Download but you can grab the latest build from Github: https://github.com/PeterHorsbollMoller/mbWindowHelper-x64. Download and unzip WindowHelper-x64.zip and run the tool in MapInfo Pro 17.0.1.