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
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
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-exchang e.com/Prog ramming/La nguages/Vi sual_Basic /A_1336-Us ing-Regula r-Expressi ons-in-Vis ual-Basic- for-Applic ations-and -Visual-Ba sic-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
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-exchang
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.
:)
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(ClassInter faceType.N one)]
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.
[ClassInterface(ClassInter
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);
}
}
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 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==
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 ;)
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
Cheers,
WC
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.
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.
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.InteropServ ices
Namespace DotNetLibrary
<ClassInterface(ClassInter faceType.A utoDual)> _
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.
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.InteropServ
Namespace DotNetLibrary
<ClassInterface(ClassInter
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
P.S.
You will select the .TLB file, not the .DLL file when you add the reference.
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
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
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
vb-proj-directory.jpg
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
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
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx