PeterBaileyUk
asked on
reg exp vbscript expression
How do i replace the first instance of the word "Boundary" in a string? also the first integer in a string?
4 ******** remove this
Boundary ******* remove this one only
Boundary
PAN EUROPEAN ST 1100 ABS
,
ST1100AV,
htmlfile.txt
4 ******** remove this
Boundary ******* remove this one only
Boundary
PAN EUROPEAN ST 1100 ABS
,
ST1100AV,
htmlfile.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
is that the .Global doing that?
ASKER
thank you
ASKER
I wonder if i am missing a reference as i am having trouble compiling it in vba
const WRITE=2 is highlighted red as is the set txtfile at the bottom:
const WRITE=2 is highlighted red as is the set txtfile at the bottom:
Sub removeodd()
Dim fso As Object
Const READ = 1
Const WRITE = 2
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtFile = fso.OpenTextFile("n:\htmlfile.txt", READ)
html = txtFile.ReadAll()
txtFile.Close
With New RegExp
.IgnoreCase = True
.Global = False
.Pattern = "Boundary"
html = .Replace(html, "")
.Pattern = "\d+"
html = .Replace(html, "")
End With
Set txtFile = fso.OpenTextFile("n:\htmlfile2.txt", WRITE)
txtFile.Write (html)
End Sub
Remove "As Object" from line 3.
ASKER
here's the effect
ee.JPG
ee.JPG
What editor are you using?
ASKER
i am in vb access
ASKER
VBscript in regexp buddy
i am in vb accessAh. That's VBA, not VBScript !!
Anyway, change the two constant names:
Const MODE_READ = 1
Const MODE_WRITE = 2
Const MODE_WRITE = 2
Be sure to change to the two usages of the constants also.
"WRITE" appears to be reserved. If you haven't done so already, you'll need to add a reference to the VBScript Regex library:
ASKER
Sorry when I posted the question I was in regex buddy and just trying to get the pattern whereas you saw that i have another question relating to html and joined the two. I added the reference and made the amendments but that set at the end is still showing red. it must be close
Sub removeodd()
Const MODE_READ = 1
Const MODE_WRITE = 2
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtFile = fso.OpenTextFile("n:\htmlfile.txt", READ)
html = txtFile.ReadAll()
txtFile.Close
With New RegExp
.IgnoreCase = True
.Global = False
.Pattern = "Boundary"
html = .Replace(html, "")
.Pattern = "\d+"
html = .Replace(html, "")
End With
Set txtFile = fso.OpenTextFile("n:\htmlfile2.txt", WRITE)
txtFile.Write (html)
End Sub
Change lines 7 and 24 to use the updated constant names.
ASKER
here is a separate sub that does some other cleaning i wonder if it can do the reg exp at the same time
Sub CleanTextFile()
Dim fs As Object, inFile, outFile
Set fs = CreateObject("scripting.filesystemobject")
Set inFile = fs.OpenTextFile("n:\htmlfile.txt", ForReading)
Set outFile = fs.CreateTextFile("n:\htmlfile_trim.txt", ForWriting)
Do While inFile.AtEndOfStream = False
strline = inFile.ReadLine
outFile.WriteLine (Trim(strline))
Loop
inFile.Close
outFile.Close
End Sub
Sub removeodd()
Const MODE_READ = 1
Const MODE_WRITE = 2
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtFile = fso.OpenTextFile("n:\htmlfile.txt", READ)
html = txtFile.ReadAll()
txtFile.Close
With New RegExp
.IgnoreCase = True
.Global = False
.Pattern = "Boundary"
html = .Replace(html, "")
.Pattern = "\d+"
html = .Replace(html, "")
End With
Set txtFile = fso.OpenTextFile("n:\htmlfile2.txt", WRITE)
txtFile.Write (html)
End Sub
ASKER
I just spotted that its not complaining now just about to run it
ASKER
its run but says it cannot find the htmlfile2.txt which makes sense as it hasnt been created yet
ASKER
thats got it...THANK YOU
ASKER