Hello all:

I would like to find all occurances of a "/" in a document and replace it
with a "," and then search for the next space (in that line) and replace it
with a ","

Example Text

100/10 hits
100/25 blocks

100,10,hits
100,25,blocks

Re: Find Replace Question by Richard

Richard
Thu Sep 14 10:01:01 CDT 2006

Epoh Rio wrote:

> I would like to find all occurances of a "/" in a document and replace it
> with a "," and then search for the next space (in that line) and replace
> it
> with a ","
>
> Example Text
>
> 100/10 hits
> 100/25 blocks
>
> 100,10,hits
> 100,25,blocks

If you expect more than one slash per line, you need a recursive function.
One solution:
==============
Option Explicit
Dim strValue

strValue = "100/10 hits then later 100/10 blocks then more"

Wscript.Echo ReplaceSlash(strValue)

Function ReplaceSlash(strLine)
Dim intK1, intK2

' Find first "/".
intK1 = InStr(strLine, "/")
If (intK1 > 0) Then
' Replace first "/" with comma.
strLine = Left(strLine, intK1 - 1) _
& "," & Mid(strLine, intK1 + 1)
' Find next space after the "/".
intK2 = InStr(intK1, strLine, " ")
If (intK2 > 0) Then
' Replace next space found with comma.
strLine = Left(strLine, intK2 - 1) _
& "," & Mid(strLine, intK2 + 1)
End If
' Call recursively only if "/" found.
strLine = ReplaceSlash(strLine)
End If
ReplaceSlash = strLine
End Function

--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net



Re: Find Replace Question by JTW

JTW
Thu Sep 14 12:33:41 CDT 2006

Here are two simple demos on how to use (VBS) Regular Expressions to
find and replace text (the "simple" for your provided string, the
"complex" for longer strings):

Set objRE = New RegExp
objRE.Global = True
objRE.IgnoreCase = True

'SIMPLE
objRE.Pattern = "(/| )"

str1 = "100/25 blocks"
str1b = objRE.Replace(str1,",")
WScript.Echo str1b

'COMPLEX
str1 = "100/10 hits in one part of the document, "
str1 = str1 & "then 100/25 blocks then more " & VbCrLf
str1 = str1 & "in another / part of the document."

objRE.Pattern = "/([\w]+) "
Set colMatches = objRE.Execute(str1)
For Each objMatch In colMatches
objRE.Pattern = "/" & objMatch.SubMatches(0) & " "
str1 = objRE.Replace(str1,"," & objMatch.SubMatches(0) & ",")
Next

WScript.Echo str1




--
Jase T. Wolfe
Dx21, LLC
http://www.Dx21.com


Epoh Rio wrote:

> Hello all:
>
> I would like to find all occurances of a "/" in a document and
> replace it with a "," and then search for the next space (in that
> line) and replace it with a ","
>
> Example Text
>
> 100/10 hits
> 100/25 blocks
>
> 100,10,hits
> 100,25,blocks

Re: Find Replace Question by Patricia

Patricia
Mon Sep 18 12:41:43 CDT 2006

This looks like I can really make use of it... is there any way to
manipulate this so the replacement string (complex) would be pulled from
another document? For example... trying to automate report-writing. I will
have a text-based or excel-based inventory (server hardware/software,
configuration info, etc) and need to pull it into a formal document. Each
customer will have a unique environment with not every customer having the
same sub-set of products deployed, and I would dearly love to write a tool
or template that updates certain fields in a Word doc with the info in the
inventory dump, when that particular item is present.

Any suggestions?


"JTW" <Newsgroup@Dx21.com> wrote in message
news:ere2sPC2GHA.4264@TK2MSFTNGP05.phx.gbl...
> Here are two simple demos on how to use (VBS) Regular Expressions to
> find and replace text (the "simple" for your provided string, the
> "complex" for longer strings):
>
> Set objRE = New RegExp
> objRE.Global = True
> objRE.IgnoreCase = True
>
> 'SIMPLE
> objRE.Pattern = "(/| )"
>
> str1 = "100/25 blocks"
> str1b = objRE.Replace(str1,",")
> WScript.Echo str1b
>
> 'COMPLEX
> str1 = "100/10 hits in one part of the document, "
> str1 = str1 & "then 100/25 blocks then more " & VbCrLf
> str1 = str1 & "in another / part of the document."
>
> objRE.Pattern = "/([\w]+) "
> Set colMatches = objRE.Execute(str1)
> For Each objMatch In colMatches
> objRE.Pattern = "/" & objMatch.SubMatches(0) & " "
> str1 = objRE.Replace(str1,"," & objMatch.SubMatches(0) & ",")
> Next
>
> WScript.Echo str1
>
>
>
>
> --
> Jase T. Wolfe
> Dx21, LLC
> http://www.Dx21.com
>
>
> Epoh Rio wrote:
>
>> Hello all:
>>
>> I would like to find all occurances of a "/" in a document and
>> replace it with a "," and then search for the next space (in that
>> line) and replace it with a ","
>>
>> Example Text
>>
>> 100/10 hits
>> 100/25 blocks
>>
>> 100,10,hits
>> 100,25,blocks



Re: Find Replace Question by Dan

Dan
Mon Sep 18 14:30:01 CDT 2006

You can assign the unique Regular Expressions pattern to a variable
that is assigned a value by reading from an external source. That
would make personalizing the data and generalizing the script easier.

Re: Find Replace Question by EpohRio

EpohRio
Mon Sep 25 16:01:02 CDT 2006

Thanks Richard & Dan

"Dan" wrote:

> You can assign the unique Regular Expressions pattern to a variable
> that is assigned a value by reading from an external source. That
> would make personalizing the data and generalizing the script easier.
>