Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - regex replace function

Reply
 
Old 08-17-2009   #1 (permalink)
James


 
 

regex replace function

I have a text file like this

"test1 a b","a b"
"test2 c d","c d"
"test3 e f","e f"
etc...

I want to strip out the quotation marks and everything after and
including the first comma to end up with this:

test1 a b
test2 c d
test3 e f
etc...

I have this code which will either strip out the first quotation mark or
strip out everything after the comma. I can't figure how to strip out
the quotations and everything after the comma in one regular expression.
Can someone help?

Thanks,



dim objFile, oFS, strOrigName, strFolder, strFileName


Set oFS = CreateObject("Scripting.FileSystemObject")


' Check that a parameter was given:
' --------------------------------

if wscript.arguments.count < 1 then
wscript.echo "This script requires 1 filename parameter"
wscript.quit 1
end if

' Make sure the named file exists:
' -------------------------------

strOrigName = wscript.arguments(0)

if oFS.FileExists( strOrigName ) = False then
wscript.echo "File " & strOrigName & " does not exist!"
wscript.quit 2
end if

Function ReplaceTest(patrn, replStr)
Dim regEx, sline, strFolder, strFileName,sSrcFSpec, tsSrc, tsDst

' Construct a copy of the source file
' --------------------------------------

strFolder = oFS.GetParentFolderName( strOrigName )
strFileName = oFS.GetFileName( strOrigName )
sSrcFSpec = strFolder & "/" & strFileName

'Open the file for reading
'---------------------------
Set tsSrc = oFS.OpenTextFile( sSrcFSpec )
WScript.Echo oFS.OpenTextFile( sSrcFSpec ).ReadAll()

'Construct a copy of the destination file
'----------------------------------------

sDstFSpec = strFolder & "/" & "no_quote_or_comma.txt"
Set tsDst = oFS.CreateTextFile( sDstFSpec, True )

' Create regular expression.

Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True

' Make replacement.
Do Until tsSrc.AtEndOfStream
sLine = Trim( tsSrc.ReadLine() )
ReplaceTest = regEx.Replace(sLine, replStr)
tsDst.writeline RePlaceTest
Loop
tsSrc.Close
tsDst.Close
End Function


rplSpec1=ReplaceTest("^[""]|["",.*]","")
rplSpec2=ReplaceTest(""",.*","")




My System SpecsSystem Spec
Old 08-18-2009   #2 (permalink)
ekkehard.horner


 
 

Re: regex replace function

James schrieb:
Quote:

> I have a text file like this
>
> "test1 a b","a b"
> "test2 c d","c d"
> "test3 e f","e f"
> etc...
>
> I want to strip out the quotation marks and everything after and
> including the first comma to end up with this:
>
> test1 a b
> test2 c d
> test3 e f
> etc...
>
> I have this code which will either strip out the first quotation mark or
> strip out everything after the comma. I can't figure how to strip out
> the quotations and everything after the comma in one regular expression.
> Can someone help?
>
> Thanks,
>
>
>
> dim objFile, oFS, strOrigName, strFolder, strFileName
>
>
> Set oFS = CreateObject("Scripting.FileSystemObject")
>
>
> ' Check that a parameter was given:
> ' --------------------------------
>
> if wscript.arguments.count < 1 then
> wscript.echo "This script requires 1 filename parameter"
> wscript.quit 1
> end if
>
> ' Make sure the named file exists:
> ' -------------------------------
>
> strOrigName = wscript.arguments(0)
>
> if oFS.FileExists( strOrigName ) = False then
> wscript.echo "File " & strOrigName & " does not exist!"
> wscript.quit 2
> end if
>
> Function ReplaceTest(patrn, replStr)
> Dim regEx, sline, strFolder, strFileName,sSrcFSpec, tsSrc, tsDst
>
> ' Construct a copy of the source file
> ' --------------------------------------
>
> strFolder = oFS.GetParentFolderName( strOrigName )
> strFileName = oFS.GetFileName( strOrigName )
> sSrcFSpec = strFolder & "/" & strFileName
>
> 'Open the file for reading
> '---------------------------
> Set tsSrc = oFS.OpenTextFile( sSrcFSpec )
> WScript.Echo oFS.OpenTextFile( sSrcFSpec ).ReadAll()
>
> 'Construct a copy of the destination file
> '----------------------------------------
>
> sDstFSpec = strFolder & "/" & "no_quote_or_comma.txt"
> Set tsDst = oFS.CreateTextFile( sDstFSpec, True )
>
> ' Create regular expression.
>
> Set regEx = New RegExp
> regEx.Pattern = patrn
> regEx.IgnoreCase = True
>
> ' Make replacement.
> Do Until tsSrc.AtEndOfStream
> sLine = Trim( tsSrc.ReadLine() )
> ReplaceTest = regEx.Replace(sLine, replStr)
> tsDst.writeline RePlaceTest
> Loop
> tsSrc.Close
> tsDst.Close
> End Function
>
>
> rplSpec1=ReplaceTest("^[""]|["",.*]","")
> rplSpec2=ReplaceTest(""",.*","")
>
>
>
Keep it simple - extract the part between " at start of line:

Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sSrcFSpec : sSrcFSpec = ".\rmtailsrc.txt"
Dim sDstFSpec : sDstFSpec = ".\rmtaildst.txt"
Dim oRE : Set oRE = New RegExp
oRE.Global = True
oRE.Multiline = True
oRE.Pattern = "^""([^""]+)""" ' between " at start of line
Dim sFAll : sFAll = oFS.OpenTextFile( sSrcFSpec ).ReadAll()
Dim tsDst : Set tsDst = oFS.CreateTextFile( sDstFSpec, True )
Dim oMTS : Set oMTS = oRE.Execute( sFAll )
Dim oMT
For Each oMT In oMTS
tsDst.WriteLine oMT.Submatches( 0 )
Next
tsDst.Close
WScript.Echo oFS.OpenTextFile( sDstFSpec ).ReadAll()

output:

=== removeTail: remove tail (and quotes) ====
test1 a b
test2 c d
test3 e f

=== removeTail: 0 done (00:00:00) ===========

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Re: Call to Regex.Replace is killing all CRLFs PowerShell
Replace function on a variable? VB Script
regex - search - replace question PowerShell
Search and replace - regex problem PowerShell
Where are things like -replace and split([regex} documented? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46