Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Regex as replace function

I am using Regular Expressions to for a replace function. I could not get
the following to work below so I changed the last line by using a Substitute
function from Excel. Why isn't the line: 'tempstring =
objRegExp.Replace(SeriesAddress, Match(0).Value) working?



Public Function RegReplace(Pattern As String, SeriesAddress As String) As
String
'Set Reference To Microsoft Scripiting Runtime
Dim objRegExp As Object
Dim Match
Dim tempstring As String

Set objRegExp = CreateObject("Vbscript.RegExp")

objRegExp.IgnoreCase = IgnoreCase
objRegExp.MultiLine = MultiLine
objRegExp.Pattern = Pattern

Set Match = objRegExp.Execute(SeriesAddress)

'tempstring = objRegExp.Replace(SeriesAddress, Match(0).Value)

tempstring = WorksheetFunction.Substitute(SeriesAddress, SeriesAddress,
Match(0).Value)

RegReplace = tempstring

End Function

Thanks

EM
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Regex as replace function

On Mon, 4 May 2009 13:01:02 -0700, Excel Monkey
wrote:

I am using Regular Expressions to for a replace function. I could not get
the following to work below so I changed the last line by using a Substitute
function from Excel. Why isn't the line: 'tempstring =
objRegExp.Replace(SeriesAddress, Match(0).Value) working?



Public Function RegReplace(Pattern As String, SeriesAddress As String) As
String
'Set Reference To Microsoft Scripiting Runtime
Dim objRegExp As Object
Dim Match
Dim tempstring As String

Set objRegExp = CreateObject("Vbscript.RegExp")

objRegExp.IgnoreCase = IgnoreCase
objRegExp.MultiLine = MultiLine
objRegExp.Pattern = Pattern

Set Match = objRegExp.Execute(SeriesAddress)

'tempstring = objRegExp.Replace(SeriesAddress, Match(0).Value)

tempstring = WorksheetFunction.Substitute(SeriesAddress, SeriesAddress,
Match(0).Value)

RegReplace = tempstring

End Function

Thanks

EM


It's hard to tell exactly what you're doing.

But you have a number of variables that are not declared.
You are doing both an extraction and then a replacement, so your routine is
more than just a simple use of the Replace method of the Regex object.

It would be useful if you could supply the values for Pattern & SeriesAddress,
as well as your expected result.

You should also make it a habit to always have Option Explicit at the start of
your modules, so that you will detect your undeclared variables. This can be
automated by selecting Tools/Options/Editor and select "Require Variable
Definition".
--ron
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 553
Default Regex as replace function

I turned Option Explicit on and changed some of my variables to Booleans.
I am just trying to parse out the file name from the network Path.

The value of StringAddress is:
='\\CGAS114\Username\My Documents\The
Folder\ExcelVBA\[TestFile.xls]Sheet1'!$A$2

The Pattern is:
\[.+\]

The line of code tempstring = objRegExp.Replace(SeriesAddress,
Match(0).Value) shold be returning:

[TestFile.xls]

As ?Match(0).Value is[ListFunctionandSubs.xls]

Below is the function again.

Public Function RegReplace(Pattern As String, SeriesAddress As String) As
String
'Set Reference To Microsoft Scripiting Runtime
Dim objRegExp As Object
Dim Match
Dim tempstring As String

Set objRegExp = CreateObject("Vbscript.RegExp")

objRegExp.IgnoreCase = True ' Was IgnoreCase
objRegExp.MultiLine = True 'Was MultiLine
objRegExp.Pattern = Pattern

Set Match = objRegExp.Execute(SeriesAddress)

tempstring = objRegExp.Replace(SeriesAddress, Match(0).Value)

'tempstring = WorksheetFunction.Substitute(SeriesAddress, SeriesAddress,
Match(0).Value)

RegReplace = tempstring

End Function

Thanks

"Ron Rosenfeld" wrote:

On Mon, 4 May 2009 13:01:02 -0700, Excel Monkey
wrote:

I am using Regular Expressions to for a replace function. I could not get
the following to work below so I changed the last line by using a Substitute
function from Excel. Why isn't the line: 'tempstring =
objRegExp.Replace(SeriesAddress, Match(0).Value) working?



Public Function RegReplace(Pattern As String, SeriesAddress As String) As
String
'Set Reference To Microsoft Scripiting Runtime
Dim objRegExp As Object
Dim Match
Dim tempstring As String

Set objRegExp = CreateObject("Vbscript.RegExp")

objRegExp.IgnoreCase = IgnoreCase
objRegExp.MultiLine = MultiLine
objRegExp.Pattern = Pattern

Set Match = objRegExp.Execute(SeriesAddress)

'tempstring = objRegExp.Replace(SeriesAddress, Match(0).Value)

tempstring = WorksheetFunction.Substitute(SeriesAddress, SeriesAddress,
Match(0).Value)

RegReplace = tempstring

End Function

Thanks

EM


It's hard to tell exactly what you're doing.

But you have a number of variables that are not declared.
You are doing both an extraction and then a replacement, so your routine is
more than just a simple use of the Replace method of the Regex object.

It would be useful if you could supply the values for Pattern & SeriesAddress,
as well as your expected result.

You should also make it a habit to always have Option Explicit at the start of
your modules, so that you will detect your undeclared variables. This can be
automated by selecting Tools/Options/Editor and select "Require Variable
Definition".
--ron

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Regex as replace function

On Thu, 7 May 2009 07:36:06 -0700, ExcelMonkey
wrote:

The value of StringAddress is:
='\\CGAS114\Username\My Documents\The
Folder\ExcelVBA\[TestFile.xls]Sheet1'!$A$2

The Pattern is:
\[.+\]

The line of code tempstring = objRegExp.Replace(SeriesAddress,
Match(0).Value) shold be returning:

[TestFile.xls]



This really is just a simple extraction.

Given that you want to return the above, using your StringAddress and Pattern,
then replace your "tempstring = ..." line with:

tempstring = Match(0)
--ron
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with a regex pattern please [email protected] Excel Programming 3 March 22nd 08 12:14 AM
Help with a Regex Pattern [email protected] Excel Programming 11 April 30th 07 01:49 AM
Regex techniques Dave Runyan Excel Programming 5 April 28th 07 12:17 AM
RegEx to parse something like this... R Avery Excel Programming 2 March 7th 05 06:41 PM
Regex Question William Barnes Excel Programming 5 January 2nd 04 11:57 AM


All times are GMT +1. The time now is 06:39 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"