Extract number from cell when specific format is found
Ron Rosenfeld wrote...
Harlan Grove wrote:
....
Back to the point, it's more efficient to replace the unwanted
contents with nothing than to use the match collection.
What do you mean by "efficient"? Easier to code? Faster execution? Something
else?
....
Regex engine does less, so entire regex use takes less time and uses
less memory. Regex .Replace method simply returns a string, but
it's .Execute method create a Matches collection object.
When is the "set re = Nothing" required?
I said paranoia. I don't entirely trust CreateObject. It does no harm
if it's not needed.
So far as your regex is concerned, I believe the lazy qualifier following the
initial "." is only required if there is more than one Ennnn substring AND the
OP wants to return the first one. . . .
Correct. It matches only up to the first E\d... pattern.
. . . With it absent, the function will return the last Ennn; and I believe the regex
engine will come to the solution in fewer steps with much less back-tracking.
Unclear. You mean that greedy * would find the last match faster than
non-greedy * would find the first? Unlikely. Also irrelevant if the
goal is the first match.
Maybe there's more backtracking in my function (picky: unlikely
there's any true backtracking, but there's A LOT of look ahead
checking), but it seems to run faster than your function. Maybe the
Matches collection overhead is that much of a drag. Also, rewriting
the regex to avoid non-greedy quantifiers requires something along the
lines of
re.Pattern = "^([^E]*(E\D)?)*E(\d+(\.\d+)?).*$"
t = re.Replace(s, "$3")
If t < vbNullString Then hg = CDbl(t) Else hg = CVErr(xlErrNum)
which involves back-to-back-to-back 0-or-more quantifiers, so probably
even more backtracking.
FWLIW, it's a pity VBScript regexs lack a simple .Find method that
returns the position of the first match in the string as a Long, or
even better something like the awk approach
result = match(s, /E[0-9]*\.?[0-9]+/) ? substr(s, RSTART, RLENGTH) :
""
Constructing and dereferencing the Matches collection takes a lot of
time.
|