Link to home
Start Free TrialLog in
Avatar of quexet
quexet

asked on

How to use ".net framework regex" in Excel

My current and only regex activities are related to Excel and VBA. As I learn more about Regex I miss in VBScript Regular Expression 5.5 a lot of the advanced regex functionality like "mode turning on and off within the regex like (?ism) and (?-ism)" - for i.e. to search "case-insensitive" and replace "case-sensitive".
Q1: Is it possible to use an other regex flavor in Excel for example ".net framework regex" and if so how do I have to accomplish that.
Q2: Am I right, that Office 2007 and 2010 do not provide more regex support?
Q3: Other solutions?

I did not find any clear answers up to now in the Web and the EE knowledge-base except something in that direction on
http://www.devx.com/DevX/Article/28468/0/page/1
but I am not familiar with Visual Studio.

I also found articles about how to use the Excel classes in .net applications but not the contrary.

Thank you for any help
Avatar of Cory Vandenberg
Cory Vandenberg
Flag of United States of America image

A1:  One possibility would be the use of an API to bring in the functionality.  It looks like the class you want is in the System.dll.  I don't have much experience when I can't find the example already online, so getting you the exact code for this is beyond me at the moment.  You might also be able to set up a Reference in the VBIDE, similar to the way you would do for VB RegEx.

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx
quexet,

While the RegExp implemented in the .Net framework is admittedly more powerful, I would urge you to look at the UDFs I include in my article here:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/A_1336-Using-Regular-Expressions-in-Visual-Basic-for-Applications-and-Visual-Basic-6.html

Those UDFs have the same functionality available in the VBScript 5.5 RegExp class. While it lacks look behind, it does include look ahead, and allows either case sensitive/case insensitive searches/replacements.

I find myself using the RegExpFind, RegExpReplace, and RegExpFindSubmatch functions from that article quite frequently.

Patrick
Patrick,

I haven't been around in so long, I completely forgot about that article of yours.  Thanks for reminding me.

:)
WC,No worries, and hope we see a little more of you around here :)Patrick
ASKER CERTIFIED SOLUTION
Avatar of Cory Vandenberg
Cory Vandenberg
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You could create a COM wrapper for the Regex functionality you seek. Here is a quick example I tested with Excel 2007. COM relies on interfaces, so that's why I created the separate interface describing the functionality provided by this wrapper. The line

    [ClassInterface(ClassInterfaceType.None)]

was added to allow the Intellisense of Office to show the method signatures. It seems you can't declare a parameterized constructor (maybe you can, but I don't have that much experience with COM), so that is why I ended up creating a separate method called SetPattern.
using System;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;

namespace RegexWrapper
{
    [ClassInterface(ClassInterfaceType.None)]
    public class Wrapper : IRegex
    {
        private Regex _regex;

        public Wrapper() { }

        public Wrapper(string pattern) { this._regex = new Regex(pattern); }

        public void SetPattern(string pattern)
        {
            this._regex = new Regex(pattern);
        }

        public bool IsMatch(string text)
        {
            if (this._regex == null)
                throw new InvalidOperationException("A pattern must be set prior to calling IsMatch.");

            return this._regex.IsMatch(text);
        }
    }

    public interface IRegex
    {
        bool IsMatch(string text);

        void SetPattern(string pattern);
    }
}

Open in new window

P.S.

I forgot to mention that you would mark the project as being "Register for COM Interop" on the Build tab of the project's properties. I believe you also need to set "Make assembly COM-Visible" on the Properteis==>Application==>Assebly Information window.
I'm pretty sure that is similar to the link I provided.
Perhaps. I started writing it before I refreshed the page...  apologies.

On the up side, if cpearson.com and wordpress.com ever get taken offline, my example will remain for the benefit of future EE'ers  ;)
No worries.  

Cheers,
WC
Avatar of quexet
quexet

ASKER

Thanks to you all for responding so fast

WarCrimes: The way you describe in your first comment is what I am looking for, but if possible without me trying to find out the details if they are already common knowledge. If you could find out more in that direction this would be great - your links to good looking Visual Studio articles are way bejond for me.

matthewspatrick: I looked into your article before - great - thanks. For a forthcoming part of the project I might be happy to use your UDFs. But now I would like to use the more sophisticated regex solution in Excel/VBA and I think, that there would be a huge community to be happy with a simple solution.

kaufmed: as mentioned no Visual Studio knowledge here.
Avatar of quexet

ASKER

After having spent some time with the free Visual Basic 2005 Express which comes with Visual Studio 2005 and trying to get the example from the article from Rich Newman or rather the equivalent solution in VB according to Mike Rosenbaum at
http://www.xtremevbtalk.com/printthread.php?t=316618
to work I am still stuck.

Code:
Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices
 
Namespace DotNetLibrary
    <ClassInterface(ClassInterfaceType.AutoDual)> _
    Public Class DotNetClass
        Public Function DotNetMethod(input As String) As String
            Return "Hello " & input
        End Function
    End Class
End Namespace

It compiles ok - no errors - but no reference in Tools/References in Excel VBA. Before I try Regasm - some respect from registry - let me ask the following:

Do I have to get a full blown Version of Visual Studio?
And if yes, which one and am I right to assume, that the 2010 versions should do it alike or better (according to the article from Rich Newmann).

Thank you for further advice.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
P.S.

You will select the .TLB file, not the .DLL file when you add the reference.
It appears Regasm is the way to go WRT to the dll showing up in the list of available references.

You should not need to get the full version of VS. Here is a screenshot of what I got after running Regasm against my sample above.
untitled1.JPG
untitled2.JPG
Avatar of quexet

ASKER

kaufmed: thank you - I did not find a tlb file - here my VS project directory content - further assistance possible? Thanks
vb-proj-directory.jpg
Avatar of quexet

ASKER

Thank you all - I was not succesful in getting Net-Regex to work in VBA - I will continue to try it in slower times.
And if somebody wrote an article on this subject I think it would be of great help for many VBA/Net-Regex wannabe's.
@matthewspatrick: your article is great - but is not what I was looking for -  I think I gave you the points in the article area
@WarCrimes: thanks for the good links - did help me to overcome my Visual Studio inhibition threshold
@kaufmed: I appreciate your understanding of my intent and your detailed information in respect to my problem