Solved

How to use ".net framework regex" in Excel

Posted on 2010-09-03
18
1,203 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:quexet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 4
  • +1
18 Comments
 
LVL 18

Expert Comment

by:Cory Vandenberg
ID: 33597482
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
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 33597555
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
0
 
LVL 18

Expert Comment

by:Cory Vandenberg
ID: 33597627
Patrick,

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

:)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 33597904
WC,No worries, and hope we see a little more of you around here :)Patrick
0
 
LVL 18

Accepted Solution

by:
Cory Vandenberg earned 250 total points
ID: 33597932
Found this cool link, but you would need Visual Studio to make this work.
http://richnewman.wordpress.com/2007/04/15/a-beginner%E2%80%99s-guide-to-calling-a-net-library-from-excel/

You also may want to look at this:
http://www.cpearson.com/excel/CreatingNETFunctionLib.aspx
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33598281
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

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33598292
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.
0
 
LVL 18

Expert Comment

by:Cory Vandenberg
ID: 33598314
I'm pretty sure that is similar to the link I provided.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33598447
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  ;)
0
 
LVL 18

Expert Comment

by:Cory Vandenberg
ID: 33598635
No worries.  

Cheers,
WC
0
 

Author Comment

by:quexet
ID: 33599761
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.
0
 

Author Comment

by:quexet
ID: 33666354
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.

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 250 total points
ID: 33674692
Have you tried using Browse under the Add Reference option in Excel? If this is a one-off solution, I think you would be fine by browsing to the appropriate DLL. If you want it to show up in the list of available references, I believe you have to do a regsvr32.exe call to register the DLL--I would need to check on this, though.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33674716
P.S.

You will select the .TLB file, not the .DLL file when you add the reference.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 33675036
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
0
 

Author Comment

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

Author Closing Comment

by:quexet
ID: 33929301
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
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question