Results 1 to 20 of 20

Thread: [RESOLVED] Creating RegEx COM wrapper

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    461

    Resolved [RESOLVED] Creating RegEx COM wrapper

    I have some application written in VB6 which uses obsolete VBScript Regular Expressions 5.5 library, and I need to use more advanced patterns which are not supported in that library. I have already asked for help in VB6 section, but I didn't get any answers, so I found this: https://msdn.microsoft.com/en-us/lib...b5000cl_topic3
    I don't have any experience in VB.NET so only you can help me to solve this problem. Therefore, I need a COM DLL (its creation is explained on above link) which will have code equivalent to this (used for VB6):
    VB Code:
    1. Function RegExpr(myPattern As String, myString As String, Optional myReplace As String, Optional bytResults As Byte, Optional intStart As Integer, Optional intCount As Integer) As Variant
    2. 'Uses "Microsoft VBScript Regular Expressions 5.5" library.
    3. On Error GoTo E
    4.     Dim objRegExp As RegExp
    5.     Set objRegExp = New RegExp
    6.     objRegExp.Pattern = myPattern
    7.     objRegExp.IgnoreCase = True
    8.     objRegExp.Global = True
    9.     If bytResults > 0 Then
    10.         Dim colMatches As MatchCollection
    11.         Set colMatches = objRegExp.Execute(myString)
    12.         If intCount = 0 Then intCount = colMatches.Count - intStart
    13.         Dim i As Integer
    14.         For i = intStart To intStart + intCount - 1
    15.             If myReplace = vbNullString Then RegExpr = RegExpr & colMatches.Item(i).Value & vbNewLine Else: RegExpr = RegExpr & objRegExp.Replace(colMatches.Item(i).Value, myReplace) & vbNewLine
    16.             If i = colMatches.Count - 1 Or bytResults = 2 Then Exit For
    17.         Next
    18.         RegExpr = Left$(RegExpr, Len(RegExpr) - 2)
    19.     Else: RegExpr = objRegExp.Test(myString)
    20.     End If
    21. E:
    22. End Function
    Thanks in advance!
    Last edited by MikiSoft; Jun 12th, 2015 at 07:35 AM.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: Creating RegEx COM wrapper

    That article is targeted to Visual Studio 2003 which is quite ancient.

    Nonetheless, what you are asking can be done relatively easily. But before we get into that, did you at least try looking for a more modern COM/ActiveX based RegEx library ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    461

    Re: Creating RegEx COM wrapper

    Yes, I've looked on official RegEx site and for VB6 there is only VBScript's library mentioned. Also I have tried to search on Google for a COM DLL, and unfortunately I found nothing which is equivalent to it.

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: Creating RegEx COM wrapper

    Ok, do you have any flavor of Visual Studio after Visual Studio 6 ? In other words, do you have VB.Net ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    461

    Re: Creating RegEx COM wrapper

    Nope, but if I need to then I'll download and install it. If you're thinking about how should I get the DLL, you can post code of the wrapper here, then compile it and send me via PM so I won't need to download the whole VS to use it only once.

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: Creating RegEx COM wrapper

    Well if you trust that I'm not gonna give you a Trojan , I'll PM you a DLL that wraps the .Net Framework's RegEx.

    You're gonna have to wait a while though. I have to write it first and I have to install VB6 on my PC to test since its been a while since I did any COM Interop stuff so I'll need to make sure I did it right.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    461

    Re: Creating RegEx COM wrapper

    Then I've found the right person for this, thanks! I will patiently wait.
    And of course that I trust you.

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: Creating RegEx COM wrapper

    Ok, Here is the DLL. Its a OneDrive link to a Zip file that contains the DLL, RegAsm which is used to register it, and two batch files for registering and unregistering the DLL.

    To use it, open the zip file and extract the folder inside. Run register.bat, which you would find inside the folder, and you will be able to use it from your VB6 application. Note this though: After registering the DLL, you must not move the folder since the COM entries in the Windows registry will be pointing to that folder. If you need to move the folder then you must unregister the DLL via
    unregister.bat first. Then you can move the folder and re-register.

    One other thing, make sure that you have the .Net Framework 3.5 or up installed on whatever PC you're using this DLL on.

    When you want to use it from VB6, you can go to the references dialog and find DotNetComRegExLib:-
    Name:  DotNet Regex.png
Views: 961
Size:  31.5 KB

    You can then use it from your VB6 application like this:-
    vb Code:
    1. '
    2.     Dim r As New DotNetRegEx
    3.     Dim matches() As String
    4.    
    5.    
    6.     r.Pattern = "whatever pattern"
    7.     matches = r.Execute("My input")

    Now, I've never used regular expressions so I don't really know anything much about it. I just exposed what you seemed to want which was pattern matching but there are a quite a few other methods like Replace and such. Just let me know if you need any of those exposed too.

    And finally, here is the .Net code for the DLL:-
    vbnet Code:
    1. Imports System.Text.RegularExpressions
    2. Imports System.Runtime.InteropServices
    3.  
    4.  
    5. <Guid("67d9031c-cb2b-40dc-99a9-f8d7bbdfa5c5")> _
    6. Public Interface IDotNetRegEx
    7.     Property Pattern As String
    8.     Property IgnoreCase As Boolean
    9.     Function Execute(ByVal input As String) As String()
    10.     Function Replace(ByVal input As String, ByVal replacement As String) As String
    11.     Function IsMatch(ByVal input As String) As Boolean
    12.  
    13. End Interface
    14.  
    15.  
    16. <ComVisible(True)> _
    17. <ClassInterface(Runtime.InteropServices.ClassInterfaceType.None)> _
    18. <Guid("90e4bc58-b2c2-4cd5-9d31-5f4d64ba6ccb")> _
    19. Public Class DotNetRegEx
    20.     Implements IDotNetRegEx
    21.  
    22.     Public Sub New()
    23.     End Sub
    24.  
    25.     Public Property Pattern As String Implements IDotNetRegEx.Pattern
    26.  
    27.     Public Property IgnoreCase As Boolean Implements IDotNetRegEx.IgnoreCase
    28.  
    29.     Public Function IsMatch(ByVal input As String) As Boolean Implements IDotNetRegEx.IsMatch
    30.         Dim reg = GetNewRegex()
    31.  
    32.         Return reg.IsMatch(input)
    33.     End Function
    34.  
    35.     Public Function Replace(ByVal input As String, ByVal replacement As String) As String Implements IDotNetRegEx.Replace
    36.         Dim reg As Regex = GetNewRegex()
    37.  
    38.         Return reg.Replace(input, replacement)
    39.     End Function
    40.  
    41.     Public Function Execute(ByVal input As String) As String() Implements IDotNetRegEx.Execute
    42.  
    43.         Dim reg As Regex = GetNewRegex()
    44.  
    45.         Dim matches As MatchCollection = reg.Matches(input)
    46.  
    47.         Return matches.Cast(Of Match).Select(Function(m) m.Value).ToArray
    48.     End Function
    49.  
    50.     Private Function GetNewRegex() As Regex
    51.         Dim options As RegexOptions
    52.  
    53.         If Me.IgnoreCase Then
    54.             options = options Or RegexOptions.IgnoreCase
    55.         End If
    56.  
    57.         Return New Regex(Me.Pattern, options)
    58.     End Function
    59.  
    60. End Class

    [UPDATE]

    I updated the DLL. The DotNetRegEx class now has the Replace method, the IsMatch method and the IgnoreCase property.

    I also updated the posted source code.
    Last edited by Niya; Jun 12th, 2015 at 12:36 PM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    461

    Re: Creating RegEx COM wrapper

    Thank you very much for the effort! Yes, I need Replace and Test command like in my example from main post; optional properties should be: Count, and if they exist - IgnoreCase and Global if they're by default set to False. And then my function will kick in.
    P.S. Can minimum requirement be lowered to .NET 2.0? RegEx class should be same for every version so it should be used on the first where it appeared.
    Last edited by MikiSoft; Jun 12th, 2015 at 10:53 AM.

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: Creating RegEx COM wrapper

    Ok, I extended it. I updated the link in post 8 so it will download the updated version. I added the Replace but there is no Test method. I assume Test checks the input to see if there are any matches ? If so, I added the IsMatch method which does this. As for counting matches, I had the Execute method return a String array so you can use UBound against that to get a count of matches.

    Quote Originally Posted by MikiSoft View Post
    P.S. Can minimum requirement be lowered to .NET 2.0? RegEx class should be same for every version so it should be used on the first where it appeared.
    I don't understand what you mean by this.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    461

    Re: Creating RegEx COM wrapper

    Thanks again, now I can implement it!

    I mean that if it can be compiled against .NET 2.0?

  12. #12
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: Creating RegEx COM wrapper

    Quote Originally Posted by MikiSoft View Post
    I mean that if it can be compiled against .NET 2.0?
    Not in its current state. I used LINQ in the source code which is not available in .Net 2.0.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    461

    Re: Creating RegEx COM wrapper

    Hm, OK then. I thought that it will be better compatible if it's compiled for .NET 2.0 because I personally have an old XP machine which has only .NET 2.0, and I think that it's not the only case when there are no new versions installed. Just don't get me wrong because I respect your work and I will definitely use this as replacement for the old RegEx library. And, have a nice day!
    Last edited by MikiSoft; Jun 12th, 2015 at 01:10 PM.

  14. #14
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: Creating RegEx COM wrapper

    .Net 3.5 comes by default with Windows 7 and higher versions come with Windows 8 and presumably Windows 10 so you can just register and use the library right away on such PCs. No version of the .Net Framework comes with XP so you're gonna have to install it, might as well install the latest one. So you don't really gain anything by having the library target 2.0. It just gives me more work since without LINQ, I'd be writing a bit more code.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    461

    Re: [RESOLVED] Creating RegEx COM wrapper

    You're right. I forgot to mark this thread as resolved, so here it is.

  16. #16
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: [RESOLVED] Creating RegEx COM wrapper

    So I take it you already tested it and it worked the way you wanted it to ? If not, lemme know what the problems are, as long as it doesn't involve the regular expressions themselves since, as I said, I have a very limited understanding of regular expressions.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2011
    Posts
    461

    Re: [RESOLVED] Creating RegEx COM wrapper

    Sorry for late response because I was hunting for and installing .Net 3.5. It's working as it should, thanks!

  18. #18
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    323

    Re: [RESOLVED] Creating RegEx COM wrapper

    here has a entended regexp engine
    deelx: http://www.regexlab.com/en/deelx/ c++ source code in it
    and here is the compiled version for vb6
    http://www.newxing.com/Tech/Program/...egExp_643.html

  19. #19
    Addicted Member
    Join Date
    Apr 2017
    Location
    India
    Posts
    234

    Re: [RESOLVED] Creating RegEx COM wrapper

    Quote Originally Posted by Niya View Post
    So I take it you already tested it and it worked the way you wanted it to ? If not, lemme know what the problems are, as long as it doesn't involve the regular expressions themselves since, as I said, I have a very limited understanding of regular expressions.
    First of all, Thanks a TON for the dll and the .net source code. I was very much looking forward for a DLL like this one.


    Well, two questions:

    1) If I use this in my app which has to be deployed at user-end, is it the same procedure for registering the tlb and/or dll during deployment too? If not and if a simpler procedure exists, I would be much grateful if you can let me know the exact steps for the same. In fact, will it be possible to use the tlb and dll without registering at user-end? If I am right, the tlb gets compiled along with my app's exe. Right?

    2) I could not find a way to find the index of the matches. Will it be possible for you please to update your tlb and DLL to give a list of all the matches' indexes too.


    Very kind regards.

  20. #20
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: [RESOLVED] Creating RegEx COM wrapper

    Quote Originally Posted by softv View Post
    1) If I use this in my app which has to be deployed at user-end, is it the same procedure for registering the tlb and/or dll during deployment too? If not and if a simpler procedure exists, I would be much grateful if you can let me know the exact steps for the same. In fact, will it be possible to use the tlb and dll without registering at user-end? If I am right, the tlb gets compiled along with my app's exe. Right?
    Yes, the procedure is the same. Unfortunately at this time I don't know how to use type libraries without registering them. It's definitely possible, the VB6 guys do it all the time but I've just never got around to learning how to do it. It's quite a bit involved. It requires a bunch of settings in a manifest file and whatnot.

    Quote Originally Posted by softv View Post
    2) I could not find a way to find the index of the matches. Will it be possible for you please to update your tlb and DLL to give a list of all the matches' indexes too.
    All I did with this project was expose .Net's Regex class through COM so VB6 applications can use it. I'm not very familiar with the Regex object itself so I don't even know if it can do that. Perhaps someone can fill me in on this. If it has a way, then this could also be exposed to VB6 for use.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width