View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default Basic regular expression question

I have a new question now. I just realized that the third element
can actually contain multiple word elements. So, my data might
actually look Like this:

"Item1 scissors"
"Item2 red notebooks"
"Item3 number #2 pencils"

So, the data format really is:

[single string of characters] [whitespace(s)] [any string of
characters and optional whitespace(s)]

So....

I plan to basically reuse the code you gave me previously, but I
need to Modify the regular expression pattern so that the variable
mc(0).submatches(1) would get assigned strings like "scissors", or
"red notebooks", or "number #2 pencils"

How should I change the pattern string?


I'm not sure if this will be helpful to you or not as I think you are
attempting to learn how to program with Regular Expressions; however,
assuming those "whitespaces" you mentioned are simply normal spaces, you can
do what you have asked without using Regular Expressions... straight VB is
enough. Here is Ron's macro revised to perform without Regular Expressions
and modified to handle the multipli-spaced data you just posted about...

Sub FooToo()
Dim i As Long, InputData(0 To 3) As String, Parts() As String, Results()
As String
InputData(0) = "Item1 scissors"
InputData(1) = "Item2 notebooks"
InputData(2) = "Item2 red notebooks"
InputData(3) = "Item3 number #2 pencils"
For i = 0 To UBound(InputData)
If InStr(InputData(i), " ") Then
Parts = Split(WorksheetFunction.Trim(InputData(i)), " ", 2)
ReDim Preserve Results(0 To 1, 0 To i)
Results(0, i) = Parts(0)
Results(1, i) = Parts(1)
End If
Next i
End Sub

Rick Rothstein (MVP - Excel)