tags:

views:

796

answers:

2

Hello All, I really dislike making Excel Macro's :-(

Here is the typical string I am working with:

·         Identify & document site-related constraints and assumptions.

I would like to scrub that string to get rid of everything before "Identify"...

I wrote a function to take the string and scrub it, here it is:

Function dataScrub(dataIn As String)
Dim dataIn_orig As String
dataIn_orig = dataIn

'BEGIN : create and set regular expression
Dim regEx
Set regEx = CreateObject("vbscript.regexp")
With regEx
        .IgnoreCase = True
        .MultiLine = False
        .Pattern = "^[\s]*[·]+[\s]*"
        .Global = True
End With

dataScrub = regEx.Replace(dataIn_orig, "")
End Function

For an unknown reason, the replace is replacing the · (not a period, more like a bullet) but not getting rid of the spaces that follow it, so my end result is:

      Identify & document site-related constraints and assumptions.

When I test my regEx using an online tester (http://www.regular-expressions.info/javascriptexample.html), it works as intended.

Any help would be greatly appreciated!

A: 

[\s] matches either a \ or an s. Try using just plain old \s without the square braces.


Well, since that still doesn't work, the last \s* might be a reluctant match instead of a greedy one. You should be able to fix that by adding a \b to the end of your expression. \b indicates a word boundary (either before or after a word). Give this one a try:

^\s*[·]+\s*\b
Welbog
i tried removing the brackets to no prevail, thank you for the suggestion though
Stu
adding the \b did not work either.of note though, it did work (as my first attempt did) in http://www.regular-expressions.info/javascriptexample.html but interestingly, did not work in http://rubular.com/ (whereas my first one did), not like any of that actually matters though :)
Stu
Are you sure the spaces are being matched by the `\s`? The space might not actually be a typical tab or space. It might be some fancy formatting instead, which a regular expression can't match. I'd advise copying the field from Excel and into a text editor, saving it, then examining the result in a hex editor to see what character(s) is actually causing that space to appear after the dot.
Welbog
great idea...i did what you suggested, here is the hex breakdown:B7 = the bulletA0 = the white space ( x7)20 = the white space immediately preceding the word "Identify"time for me to look up how to incorporate hex values into Excel VB regEx's (if it is possible)
Stu
FTW!doing regEx's with HEX was pretty easy after a quick google search. Welbox, thank you very much for you time assisting me!
Stu
Glad I could help!
Welbog
A: 

Looks like the answer has been accepted already, but running this line of code in your function will take care of you issue as well.

Cells.Replace What:=Chr(160), Replacement:="", LookAt:=xlPart
Irwin M. Fletcher
thank you, i will consider trying that approach in the future
Stu