Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Help with a Regex Pattern

I have data which is in this format:

J 123 K

The J is optional, could be upper or lower case, and could be a J or
an X or a Z.

It is always followed by a space. The three digits. Then a space.
Then any alphabetic character upper or lower case, but not optional,
the character must be there.

I am having trouble with the pattern, could someone show me how to set
it up?

tia
bob


Const sPattern As String = "([JjXxZz]\s)?\d{1,3}(?=\ )(\D\s)"

Set oRegex = New RegExp
oRegex.Pattern = sPattern
oRegex.Global = True

'check all of the rows
With ActiveSheet
S = .Cells(lR, iCWD).Value2
If oRegex.Test(S) = True Then
Set colmatches = oRegex.Execute(S)
strData = colmatches(0)
'convert the dewey to numeric if it does not
'have a leading alpha
If (bStringsT_IsLongInteger(strData) = True) Then
.Cells(lR, iCO).Value2 =
iStringsT_StringToIntegerNumber(strData)
Else
.Cells(lR, iCO).Value2 = colmatches(0)
End If
End If
End With

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,090
Default Help with a Regex Pattern

What do you mean you "are having trouble with the pattern"? What are you
trying to do? What do you want to see happen? What do you want Excel to do
for you? HTH Otto
wrote in message
ups.com...
I have data which is in this format:

J 123 K

The J is optional, could be upper or lower case, and could be a J or
an X or a Z.

It is always followed by a space. The three digits. Then a space.
Then any alphabetic character upper or lower case, but not optional,
the character must be there.

I am having trouble with the pattern, could someone show me how to set
it up?

tia
bob


Const sPattern As String = "([JjXxZz]\s)?\d{1,3}(?=\ )(\D\s)"

Set oRegex = New RegExp
oRegex.Pattern = sPattern
oRegex.Global = True

'check all of the rows
With ActiveSheet
S = .Cells(lR, iCWD).Value2
If oRegex.Test(S) = True Then
Set colmatches = oRegex.Execute(S)
strData = colmatches(0)
'convert the dewey to numeric if it does not
'have a leading alpha
If (bStringsT_IsLongInteger(strData) = True) Then
.Cells(lR, iCO).Value2 =
iStringsT_StringToIntegerNumber(strData)
Else
.Cells(lR, iCO).Value2 = colmatches(0)
End If
End If
End With



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Help with a Regex Pattern

I haven't try it, but I thought it would be this

Const sPattern As String = "([JjXxZz]?)d{1,3}(?=\ )[A-Za-z]"




" wrote:

I have data which is in this format:

J 123 K

The J is optional, could be upper or lower case, and could be a J or
an X or a Z.

It is always followed by a space. The three digits. Then a space.
Then any alphabetic character upper or lower case, but not optional,
the character must be there.

I am having trouble with the pattern, could someone show me how to set
it up?

tia
bob


Const sPattern As String = "([JjXxZz]\s)?\d{1,3}(?=\ )(\D\s)"

Set oRegex = New RegExp
oRegex.Pattern = sPattern
oRegex.Global = True

'check all of the rows
With ActiveSheet
S = .Cells(lR, iCWD).Value2
If oRegex.Test(S) = True Then
Set colmatches = oRegex.Execute(S)
strData = colmatches(0)
'convert the dewey to numeric if it does not
'have a leading alpha
If (bStringsT_IsLongInteger(strData) = True) Then
.Cells(lR, iCO).Value2 =
iStringsT_StringToIntegerNumber(strData)
Else
.Cells(lR, iCO).Value2 = colmatches(0)
End If
End If
End With


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Help with a Regex Pattern

Otto: It is obvious from the code he is working with C++. The patterns he is
working with goes back to the development of UNIX at Bell Labs (and probably
earlier than that). The syntax that Bob is using describes a custom language
that is used to describe strings consisting of words and characters.

Bell Labs did lots of research on Pattern Recognition like this to develope
efficient methods for searching for name in a Phone Book. Computers were
very slow and memory was very expensive in the 1970's. Bell Labs were
trying to save money by by find efffiecent methods for storing their phone
books electronically and finding name quickly using computers so operators
didn't have to manually look up people names.

That was one of the main reasons UNIX was developed. Bell labs had lots of
computer systems that couldn't talk to each other (phonebook, billing,
switching equipment) and wanted to develope one computer language that could
be used by all there computers.

"Otto Moehrbach" wrote:

What do you mean you "are having trouble with the pattern"? What are you
trying to do? What do you want to see happen? What do you want Excel to do
for you? HTH Otto
wrote in message
ups.com...
I have data which is in this format:

J 123 K

The J is optional, could be upper or lower case, and could be a J or
an X or a Z.

It is always followed by a space. The three digits. Then a space.
Then any alphabetic character upper or lower case, but not optional,
the character must be there.

I am having trouble with the pattern, could someone show me how to set
it up?

tia
bob


Const sPattern As String = "([JjXxZz]\s)?\d{1,3}(?=\ )(\D\s)"

Set oRegex = New RegExp
oRegex.Pattern = sPattern
oRegex.Global = True

'check all of the rows
With ActiveSheet
S = .Cells(lR, iCWD).Value2
If oRegex.Test(S) = True Then
Set colmatches = oRegex.Execute(S)
strData = colmatches(0)
'convert the dewey to numeric if it does not
'have a leading alpha
If (bStringsT_IsLongInteger(strData) = True) Then
.Cells(lR, iCO).Value2 =
iStringsT_StringToIntegerNumber(strData)
Else
.Cells(lR, iCO).Value2 = colmatches(0)
End If
End If
End With




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Help with a Regex Pattern

Joel,

As I understood the OP's request, both the initial letter and the subsequent
space are optional.

The "?" indicates that everything in the preceding is optional, since the
preceding is enclosed in parentheses.

So either

J 123 K

or

123 K

would be acceptable.

--ron


On Sun, 29 Apr 2007 07:48:01 -0700, Joel
wrote:

Ron: Shouldn't the '?' be optional? this would be the space after the first
optional character.

"Ron Rosenfeld" wrote:

On 29 Apr 2007 06:55:01 -0700, wrote:

I have data which is in this format:

J 123 K

The J is optional, could be upper or lower case, and could be a J or
an X or a Z.

It is always followed by a space. The three digits. Then a space.
Then any alphabetic character upper or lower case, but not optional,
the character must be there.


([JjXxZz]\s)?\d{3}\s[A-Za-z]




I am having trouble with the pattern, could someone show me how to set
it up?

tia
bob


--ron


--ron
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Help with a Regex Pattern

I though the ? represent any one character. I would think these would be
better

([JjXxZz][ ]\s)\d{3}\s[ =][A-Za-z]


"Ron Rosenfeld" wrote:

Joel,

As I understood the OP's request, both the initial letter and the subsequent
space are optional.

The "?" indicates that everything in the preceding is optional, since the
preceding is enclosed in parentheses.

So either

J 123 K

or

123 K

would be acceptable.

--ron


On Sun, 29 Apr 2007 07:48:01 -0700, Joel
wrote:

Ron: Shouldn't the '?' be optional? this would be the space after the first
optional character.

"Ron Rosenfeld" wrote:

On 29 Apr 2007 06:55:01 -0700, wrote:

I have data which is in this format:

J 123 K

The J is optional, could be upper or lower case, and could be a J or
an X or a Z.

It is always followed by a space. The three digits. Then a space.
Then any alphabetic character upper or lower case, but not optional,
the character must be there.

([JjXxZz]\s)?\d{3}\s[A-Za-z]




I am having trouble with the pattern, could someone show me how to set
it up?

tia
bob

--ron


--ron

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 947
Default Help with a Regex Pattern

I have data which is in this format:
J 123 K


([JjXxZz]\s)?\d{3}\s[A-Za-z]


Just an idea. My thoughts might be to limit the search by including "^" and
"$"

"^([JjXxZz]\s)?\d{3}\s[A-Za-z]$"

What I'm thinking is that maybe something like
"A 123 k" might test True by ignoring the "A",
and
"ABC j 123 k" might test True by finding the pattern within a larger
string.

--
Dana DeLouis
Windows XP & Office 2007


"Ron Rosenfeld" wrote in message
...
On 29 Apr 2007 06:55:01 -0700, wrote:

I have data which is in this format:

J 123 K

The J is optional, could be upper or lower case, and could be a J or
an X or a Z.

It is always followed by a space. The three digits. Then a space.
Then any alphabetic character upper or lower case, but not optional,
the character must be there.


([JjXxZz]\s)?\d{3}\s[A-Za-z]




I am having trouble with the pattern, could someone show me how to set
it up?

tia
bob


--ron



  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Help with a Regex Pattern

On Sun, 29 Apr 2007 15:04:07 -0400, "Dana DeLouis"
wrote:

What I'm thinking is that maybe something like
"A 123 k" might test True by ignoring the "A",
and
"ABC j 123 k" might test True by finding the pattern within a larger
string.


I guess one question which has not been answered is the reason for the regex.
Is it to extract any matching pattern from the string; or is it to ensure that
only the desired pattern is present.

If the latter, then your expression is appropriate.
--ron


  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Help with a Regex Pattern

On Sun, 29 Apr 2007 10:56:01 -0700, Joel
wrote:

I though the ? represent any one character.


That is incorrect. At least with VBScript flavor of Regular Expressions, as
used in my expression, it indicates that the preceding (enclosed in parentheses
(is optional). To be more precise, it matches the preceding character or
subexpression zero or one time, so it is equivalent to {0,1}

It can also indicate a non-greedy quantifier.
--ron
  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default Help with a Regex Pattern

On Sun, 29 Apr 2007 16:17:10 -0400, Ron Rosenfeld
wrote:

On Sun, 29 Apr 2007 15:04:07 -0400, "Dana DeLouis"
wrote:

What I'm thinking is that maybe something like
"A 123 k" might test True by ignoring the "A",
and
"ABC j 123 k" might test True by finding the pattern within a larger
string.


I guess one question which has not been answered is the reason for the regex.
Is it to extract any matching pattern from the string; or is it to ensure that
only the desired pattern is present.

If the latter, then your expression is appropriate.
--ron



Reading the OP's post again (and looking at his macro), it does seem as if he
wants to either match, or not match, the specific pattern. So your addition of
^ and $ would indeed be warranted.

And it's also possible to detect the format without regular expressions:

===============================
For Each c In rRng
If c.Text Like "[JjXxZz] ### [A-Za-z]" Or _
c.Text Like "### [A-Za-z]" Then
Debug.Print c.Text, "TRUE"
Else
Debug.Print c.Text, "FALSE"
End If
Next c
================================
--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
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 Replacement patterns CodeSponge[_2_] Excel Programming 2 February 5th 05 07:39 AM
RegEx in VBE code editor R Avery Excel Programming 6 December 21st 04 01:58 PM
Regex Question William Barnes Excel Programming 5 January 2nd 04 11:57 AM


All times are GMT +1. The time now is 01:23 AM.

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"