Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expression/replace spaces

Status
Not open for further replies.

TheInsider

Programmer
Jul 17, 2000
796
0
0
CA
Hello,
I am looking for the most efficient method to replace one or more white space characters within a given string with another string or character, say "_". The problem is I only want to replace spaces that aren't between double (" ") quotes.

i.e. replace the following string:
Code:
star "big small " water flower "hat mouse"
with:
Code:
star_"big small "_water_flower_"hat mouse"
I am using the vbscript regular expression library, but so far I cannot get the desired result. I have tried
Code:
Pattern = "[^""]\s+[^""]"
and various other things, like comparing matches in a matchcollection with
Code:
.Execute
, but have had no luck.
Thanks.

 
Someone else will probably put out a better Idea. But this StoneAge method does work!

In a form place two textboxes and a button:


Option Explicit

Const DSPACE = 32
Const DQuat = 34
Const DUNDERLINE = 95

Private Sub Command1_Click()
Dim i As Integer
Dim Number As Integer
Dim Flag As Integer
Dim ArrByte() As Byte

Number = LenB(Text1)
ReDim ArrByte(Number)
ArrByte = Text1
Flag = 0

For i = 0 To Number - 1
If ArrByte(i) = DQuat Then Flag = Flag + 1
If Flag = 2 Then Flag = 0
If Flag = 0 Then
If ArrByte(i) = DSPACE Then ArrByte(i) = DUNDERLINE
End If
Next i

Text2 = ArrByte
End Sub
 
Public Function strOut(Optional ByVal strIn As String = "", Optional ByVal strChange As String = "") As String
Dim pos As Integer
strIn = Trim(strIn)
strOut = ""
If Len(strIn) = 0 Then
Exit Function
End If
pos = InStr(1, strIn, " ", vbBinaryCompare)
While pos > 0
strIn = Mid(strIn, 1, pos - 1) & strChange & Mid(strIn, pos + 1)
pos = InStr(1, strIn, " ", vbBinaryCompare)
Wend
strOut = strIn
End Function
 
Thanks for the help chiuchimu and 1127. I will give it a try, but due to the speed advantage of using regular expressions, I will probably continue to work on finding a solution that uses them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top