tags:

views:

44

answers:

2
Dim sString

sString = "John;Mary;Anne;Adam;Bill;Ester"

Is there a regex I can use to retrieve the following from the above list:

  1. John (; at the end of the name)
  2. Anne (; at the beginning and end)
  3. Ester (; at the beginning)

I am currently using the following regex for each:

1. Joh.*
2. .*An.*
3. .*st.*

But, the above retrieves the entire string instead of the values I want. How can I get the correct values?

Code:

Dim oRegex : Set oRegex = New RegExp
oRegex.Global = False
oRegex.IgnoreCase = False

'John
oRegex.Pattern = "Joh.*"

Set oMatch = oRegex.Execute(sString)
sName = oMatch(0)

The above code retrieves the entire string, instead of only John. Same issue with the others :(

+2  A: 

Why not just use this?

Dim nameArray = Split(sString,";")
Eric
Thanks for the quick response Eric.I'm not sure it because the string is HUGE and using Split causes a big performance overhead..
Karthik
Not as much as the overhead from a regex.
Eric
+1  A: 

easier way, do a split() on your string. Then iterate the list to get your stuff eg

s = split(sString,";")
for i=Lbound(s) to Ubound(s)
  ' get your names
next
ghostdog74
Thanks ghostdog74. I'm not sure it because the string is HUGE and using Split causes a big performance overhead.
Karthik
it should not be a big performance overhead.
ghostdog74