Link to home
Start Free TrialLog in
Avatar of PeterBaileyUk
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
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PeterBaileyUk
PeterBaileyUk

ASKER

can I just confirm that only the very first instance of the word "Boundary" is removed and not every one?
is that the .Global doing that?
Yes.

Yes. If you set "Global = True", then every occurrence would be replaced.

User generated image
thank you
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:

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

Open in new window

Remove "As Object" from line 3.
here's the effect
ee.JPG
What editor are you using?
i am in vb access
VBscript in regexp buddy
i am in vb access
Ah. That's VBA, not VBScript  !!

Anyway, change the two constant names:

Const MODE_READ = 1
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:

User generated image
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

Open in new window

Change lines 7 and 24 to use the updated constant names.
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

Open in new window

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

Open in new window

I just spotted that its not complaining now just about to run it
its run but says it cannot find the htmlfile2.txt which makes sense as it hasnt been created yet
thats got it...THANK YOU