karagota
karagota

Reputation: 53

How to remove non-alphabetic characters from start of string until the first alphabetic character?

I need a code in VBScript to trim a string from start until the first alphabetic character:

1) №123 John Doe. Room 1.

The result should be

John Doe. Room 1.

If I use this code:

Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True   
objRegEx.Pattern = "[^A-Za-z\n\r]"
strSearchString = objRegEx.Replace(strSearchString, "")

I trim characters not only from start, but from everywhere. That is not what I need. What method should I use to trim only starting symbols?

P.S. This code roughly does what I need, but is not ultimately elegant:

objRegEx.Global = True   
objRegEx.Pattern = "[a-zA-Z]"
Set Matches =  objRegEx.Execute(strSearchString)
FirstMatch = Matches(0).FirstIndex
MyString = Mid(strSearchString, FirstMatch+1)

Upvotes: 0

Views: 2075

Answers (2)

MC ND
MC ND

Reputation: 70971

strSearchString = "№123 John Doe. Room 1."

With New RegExp
    .Pattern = "^[^a-zA-Z]*"
    strSearchString = .Replace(strSearchString, "")
End With

WScript.Echo strSearchString

From the start of the value, replace any sequence of characters not included in the indicated set.

Upvotes: 3

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38775

Use ^ (start of string) to anchor the pattern at the start of the string. E.g.:

>> set r = New RegExp
>> r.Pattern = "^\D*\d+\s*"
>> Wscript.Echo qq(r.replace("№123 John Doe. Room 1.", ""))
>>
"John Doe. Room 1."

As your specs aren't entirely clear, the pattern may need further work.

Upvotes: 2

Related Questions