![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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(""",.*","") > > > 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 Specs![]() |
![]() |
| 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 | |||