Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,388
Default Regular expressions

Hello,

The snippet of code below builds a new string "Newval" based on the pattern
[A-Z]
and that works fine but what if I wanted to exclude values .
For example I could set a pattern of "[)(*&^]" how would I code it to
exclude those values?

With RegExp
.Global = True
.Pattern = "[A-Z]"
End With
Set Myrange = ActiveSheet.Range("A1:A10")
For Each C In Myrange
newval = ""
Set Collection = RegExp.Execute(C.Value)
For Each RegMatch In Collection
newval = newval & RegMatch
Next
Next


D
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 595
Default Regular expressions

On Wed, 19 Sep 2007 12:24:02 -0700, Dave
wrote:

For example I could set a pattern of "[)(*&^]" how would I code it to
exclude those values?


.Pattern = "[^)(*&\^]"

Note the \ that precedes the second ^. The first ^ defines the character
sequence as negative and the \ escapes the second ^ so it's included in the
negative character sequence.
--
Dick Kusleika
Microsoft MVP-Excel
http://www.dailydoseofexcel.com
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Regular expressions

On Wed, 19 Sep 2007 12:24:02 -0700, Dave
wrote:

Hello,

The snippet of code below builds a new string "Newval" based on the pattern
[A-Z]
and that works fine but what if I wanted to exclude values .
For example I could set a pattern of "[)(*&^]" how would I code it to
exclude those values?

With RegExp
.Global = True
.Pattern = "[A-Z]"
End With
Set Myrange = ActiveSheet.Range("A1:A10")
For Each C In Myrange
newval = ""
Set Collection = RegExp.Execute(C.Value)
For Each RegMatch In Collection
newval = newval & RegMatch
Next
Next


D


Perhaps you just need to negate the character class:

"[^)(*&^]"

which translates as:

Match a single character NOT present in the list
")(*&^"


--ron
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Regular expressions

On Wed, 19 Sep 2007 17:59:04 -0500, Dick Kusleika wrote:

On Wed, 19 Sep 2007 12:24:02 -0700, Dave
wrote:

For example I could set a pattern of "[)(*&^]" how would I code it to
exclude those values?


.Pattern = "[^)(*&\^]"

Note the \ that precedes the second ^. The first ^ defines the character
sequence as negative and the \ escapes the second ^ so it's included in the
negative character sequence.


I don't believe there is any need to escape the ^ in VBScript. Both by
testing, and by documentation, it seems to be unnecessary.

http://msdn2.microsoft.com/en-us/library/ta6y6h4z.aspx

"If the caret character appears in any other position within the list, it
matches itself, that is, it has no special meaning. "


--ron
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Regular expressions

On Wed, 19 Sep 2007 20:14:28 -0400, Ron Rosenfeld
wrote:

On Wed, 19 Sep 2007 17:59:04 -0500, Dick Kusleika wrote:

On Wed, 19 Sep 2007 12:24:02 -0700, Dave
wrote:

For example I could set a pattern of "[)(*&^]" how would I code it to
exclude those values?


.Pattern = "[^)(*&\^]"

Note the \ that precedes the second ^. The first ^ defines the character
sequence as negative and the \ escapes the second ^ so it's included in the
negative character sequence.


I don't believe there is any need to escape the ^ in VBScript. Both by
testing, and by documentation, it seems to be unnecessary.

http://msdn2.microsoft.com/en-us/library/ta6y6h4z.aspx

"If the caret character appears in any other position within the list, it
matches itself, that is, it has no special meaning. "


--ron


Of course, that only applies when the ^ appears within brackets as other than
the first character.
--ron


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 595
Default Regular expressions

On Wed, 19 Sep 2007 20:14:28 -0400, Ron Rosenfeld
wrote:

On Wed, 19 Sep 2007 17:59:04 -0500, Dick Kusleika wrote:

On Wed, 19 Sep 2007 12:24:02 -0700, Dave
wrote:

For example I could set a pattern of "[)(*&^]" how would I code it to
exclude those values?


.Pattern = "[^)(*&\^]"

Note the \ that precedes the second ^. The first ^ defines the character
sequence as negative and the \ escapes the second ^ so it's included in the
negative character sequence.


I don't believe there is any need to escape the ^ in VBScript. Both by
testing, and by documentation, it seems to be unnecessary.

http://msdn2.microsoft.com/en-us/library/ta6y6h4z.aspx

"If the caret character appears in any other position within the list, it
matches itself, that is, it has no special meaning. "


Ron: You're right. Mine was actually excluding the slash too. Thanks for
the correction.
--
Dick
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
Regular Expressions sl Excel Programming 2 January 22nd 07 11:27 PM
Using Regular Expressions with VBA Andrew Hall NZ Excel Programming 5 November 21st 06 09:30 PM
Regular expressions JeffMelton Excel Programming 2 March 1st 06 12:52 AM
Regular Expressions in VBA? Rob[_23_] Excel Programming 13 February 10th 05 05:34 AM
VBA and Regular expressions Friedrich Muecke Excel Programming 3 October 3rd 03 01:46 AM


All times are GMT +1. The time now is 08:52 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"