View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Walter Briscoe Walter Briscoe is offline
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