Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 100
Default RegExp not matching what I want

This is my regular expression:
lineRegEx.Pattern = " *([^ ]+) ?(.*) *"

What I want is for it to strip leading and trailing spaces, and then
split the remainder on the first space that it encounters. What it
does in practice is to return the entire string as the first match.
Any ideas what I'm doing wrong?

I've tested it he
http://www.regular-expressions.info/...ptexample.html

....and it shows the same behaviour, but the same pattern works he
http://www.regular-expressions.info/...ptexample.html

I can only assume that VBA is not using the regular expression
implementation that I have been led to expect.

Phil Hibbs.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 100
Default RegExp not matching what I want

Aha, I fixed my problem, I was iterating over the Matches instead of
the Matches.SubMatches!

Phil.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 279
Default RegExp not matching what I want

You don't show enough code to figure out what was wrong.
I'm glad you sorted it out.
The act of posting often stimulates thought.

I find it helpful to develop Regular Expression code in the immediate
window. The test method is useful. It is also useful to put the pattern
into English. e.g. zero or more spaces; remember one of more non-spaces;
an optional space; remember zero or more characters; zero or more
spaces. I would be inclined to try "^ *([^ ]+) ?(.*) *$". ^ means "start
of string" and $ means "end of string". I can then use "$1" and "$2" in
the replace method to show the remembered chunks. I suspect the "?" is
superfluous. It is good to see someone else using regular expressions. I
find them clearer than a sequence of left, mid and right calls. Also,
would you expect to match "ABC"? i.e. there is no space to split on.

In message
s.com of Mon, 19 Apr 2010 07:14:04 in microsoft.public.excel.programmin
g, Phil Hibbs writes
This is my regular expression:
lineRegEx.Pattern = " *([^ ]+) ?(.*) *"

What I want is for it to strip leading and trailing spaces, and then
split the remainder on the first space that it encounters. What it
does in practice is to return the entire string as the first match.
Any ideas what I'm doing wrong?

I've tested it he
http://www.regular-expressions.info/...ptexample.html

...and it shows the same behaviour, but the same pattern works he
http://www.regular-expressions.info/...ptexample.html

I can only assume that VBA is not using the regular expression
implementation that I have been led to expect.

Phil Hibbs.


--
Walter Briscoe
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default RegExp not matching what I want

If I understand what you are trying to do, you can do it without using
regular expressions. I think you are saying you want to end up with an array
containing two elements... the first "word" (in front of the first space)
after leading and trailing spaces have been removed in the array's first
element and all the rest of the text in the array's second element. If that
is correct, then give this code a try (just substitute your text or a
variable/range/whatever that contains your text where I have written
"YourTextGoesHere")...

Dim Result() As String
.......
.......
' This is the only important line of code
Result =Split(Trim(YourTextGoesHere), " ", 2)
.......
.......
' These two lines are only here to show you it worked
MsgBox "First word: " & Result(0)
MsgBox "Rest of text: " & Result(1)

Note: The Split function will **always** return a zero-based array even if
you are using an "Option Base 1" statement.

--
Rick (MVP - Excel)



"Phil Hibbs" wrote in message
...
This is my regular expression:
lineRegEx.Pattern = " *([^ ]+) ?(.*) *"

What I want is for it to strip leading and trailing spaces, and then
split the remainder on the first space that it encounters. What it
does in practice is to return the entire string as the first match.
Any ideas what I'm doing wrong?

I've tested it he
http://www.regular-expressions.info/...ptexample.html

...and it shows the same behaviour, but the same pattern works he
http://www.regular-expressions.info/...ptexample.html

I can only assume that VBA is not using the regular expression
implementation that I have been led to expect.

Phil Hibbs.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 100
Default RegExp not matching what I want

Rick Rothstein wrote:
If I understand what you are trying to do, you can do it without using
regular expressions.


Sure, but I was replacing my current implementation with a regexp
version to see if it was more efficient. I thought that maybe a single
regexp call that returns the elements and their lengths would be more
efficient than separate trim, instr, mid, and length calls (or trim,
split, length calls in your suggeston). Turns out it isn't, partly
because I misunderstood the length thing. You don't get the lengths of
the sub-matches, just the length of the full expression match, so I
had to do the length call anyway, and the "rest of" can be very long
(up to 3MB).

Phil Hibbs.


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default RegExp not matching what I want

On Mon, 19 Apr 2010 07:14:04 -0700 (PDT), Phil Hibbs wrote:

This is my regular expression:
lineRegEx.Pattern = " *([^ ]+) ?(.*) *"

What I want is for it to strip leading and trailing spaces, and then
split the remainder on the first space that it encounters. What it
does in practice is to return the entire string as the first match.
Any ideas what I'm doing wrong?

I've tested it he
http://www.regular-expressions.info/...ptexample.html

...and it shows the same behaviour, but the same pattern works he
http://www.regular-expressions.info/...ptexample.html

I can only assume that VBA is not using the regular expression
implementation that I have been led to expect.

Phil Hibbs.


Phill,

If I might suggest:

"^\s*(\S+)\s(.*?)\s*$"

or, if you don't want to return any leading spaces prior to the second
submatch:

"^\s*(\S+)\s+(.*?)\s*$"



--ron
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 690
Default RegExp not matching what I want

On 4/20/2010 5:31 AM, Phil Hibbs wrote:
Rick Rothstein wrote:
If I understand what you are trying to do, you can do it without using
regular expressions.


Sure, but I was replacing my current implementation with a regexp
version to see if it was more efficient. I thought that maybe a single
regexp call that returns the elements and their lengths would be more
efficient than separate trim, instr, mid, and length calls (or trim,
split, length calls in your suggeston). Turns out it isn't, partly
because I misunderstood the length thing. You don't get the lengths of
the sub-matches, just the length of the full expression match, so I
had to do the length call anyway, and the "rest of" can be very long
(up to 3MB).

Phil Hibbs.



Hi. Don't know if this is any faster...

Sub Demo()
Dim S As String
Dim v(1 To 2)
Dim P As Long

S = " This is a test "
S = Trim(S)
P = InStr(1, S, Space(1))
v(1) = Left$(S, P - 1)
v(2) = Mid$(S, P + 1)
End Sub

= = = = = = =
HTH :)
Dana DeLouis
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 100
Default RegExp not matching what I want

Ron Rosenfeld wrote:
If I might suggest:
"^\s*(\S+)\s(.*?)\s*$"


It wasn't the regular expression that was wrong, it's the way I was
using the return value. But that's probably a better regex than mine.

Phil Hibbs.
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 100
Default RegExp not matching what I want

Dana DeLouis wrote:
Hi. *Don't know if this is any faster...


That's almost exactly what my original code did. It has to process the
string three times (if you add a length call at the end), I was trying
to reduce the number of times.

Phil Hibbs.
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default RegExp not matching what I want

On Wed, 21 Apr 2010 05:47:20 -0700 (PDT), Phil Hibbs wrote:

Ron Rosenfeld wrote:
If I might suggest:
"^\s*(\S+)\s(.*?)\s*$"


It wasn't the regular expression that was wrong, it's the way I was
using the return value. But that's probably a better regex than mine.

Phil Hibbs.


OIC

By the way, reading through some other messages in the thread, I was wondering
about why you needed the LENgth calls? Is this for some subsequent processing
of the outputs? Because if you just want to trim off leading and trailing
spaces, that can be done with the regex.
--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
RegExp & VB Noob Qs Ron Rosenfeld Excel Programming 2 June 28th 08 12:47 AM
RegExp Arshavir Grigorian Excel Programming 2 March 13th 08 12:55 AM
RegExp information Alain R. Excel Programming 1 February 18th 08 02:56 PM
RegExp - String in VBA MSK Excel Programming 0 July 24th 07 02:50 PM
pattern matching w/o using VBScript's RegExp Object Tim Williams Excel Programming 0 March 7th 05 09:19 PM


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