MapInfo Pro Developers User Group

 View Only
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

    Employee
    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

    Employee
    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)

    zip
    WindowHelperAssembly.zip   16 KB 1 version


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

    Employee
    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

    Employee
    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

    Employee
    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.



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

    Posted 03-20-2019 05:08
      |   view attached
    I had use for this again after a while and it's working fine in MI 17 now.

    I am coming across one issue though in defining what to do when a regex pattern isn't available.

    Currently it works as
    Update tempTable Set ScratchField = RegExReplace(inColumn,regexString,"")

    Where the "" just puts in a blank line where there's no match. If I put the last value as 'inColumn' it then doesn't do any changes.

    Within the dll is it possible to tell it to only replace the value if there is a match and if not to use the value from the input field?

    I've attached my current code sample.




    ------------------------------
    George Corea
    Mangoesmapping
    ------------------------------

    Attachment(s)



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

    Posted 02-03-2021 06:18
    I have been trying to use this in v2019 and the RegExFirstMatch doesn't produce what's needed.

    Given
    3759582-v1-148607 989491 8 Kalu Cl.PDF

    The regex is meant to extract '989491'.
    The pattern for this is "^\d+-v1-\s?\d+\s*(\d+)"

    In 32bit I get the correct output but in 64bit I get 3759582-v

    With the introduction of python into v2019, can I now leave the dll and run regex to update a column to get something like

    Include "MapBasic.def"
    Include "Menu.def"

    '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

    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

    Dim sInput, sPattern As String

    Select * from Building_List where Document_Name like "%-v1%" into building_temp
    'Update building_temp Set PCL = RegExMatch(Document_Name,"^\d+-v1-\s?\d+\s*(\d+)") '\\32bit code
    sPattern = "^\d+-v1-\s?\d+\s*(\d+)"
    Update building_temp Set PCL = RegExFirstMatch(Document_Name, sPattern) '\\64bit code

    Thanks,

    ------------------------------
    George Corea
    Mangoesmapping
    MOOMIN QLD
    ------------------------------



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

    Employee
    Posted 02-09-2021 03:40
    Hi George

    Didn't you get the Regex to work in v17 as you stated in a previous post?
    I earlier suggested using this pattern instead but maybe that doesn't give you what you are looking for?

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

    The WindowHelper DLL basically exposes the Regex functions from .NET:
    #region RegEx
    //<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 "";
    }

    #endregion RegEx

    Python also has support for Regex, maybe via an additional Python module.
    But I'm not sure you can combine MapBasic and Python in for example an Update statement.
    But maybe I'm overlooking the obvious.

    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------



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

    Employee
    Posted 10-03-2022 04:06
    PS: MapInfo Pro v2021 now has built-in support for Regex via these functions:
    • RegExReplace$(): This function replaces all or single RegEx matches in a string.
    • RegExMatch$(): This function matches the given RegEx pattern with the input string.
    • RegExSearch$(): This function gets the index of first the substring matching the given RegEx pattern with the input string.


    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------