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
PeterBaileyUkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
You can use the RegExp object with the Global property set to False.

e.g.

Const READ = 1
Const WRITE = 2

Set fso = CreateObject("Scripting.FileSystemObject")
Set txtFile = fso.OpenTextFile("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("htmlfile2.txt", WRITE)

txtFile.Write(html)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PeterBaileyUkAuthor Commented:
can I just confirm that only the very first instance of the word "Boundary" is removed and not every one?
0
PeterBaileyUkAuthor Commented:
is that the .Global doing that?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

käµfm³d 👽Commented:
Yes.

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

Screenshot
0
PeterBaileyUkAuthor Commented:
thank you
0
PeterBaileyUkAuthor Commented:
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

0
käµfm³d 👽Commented:
Remove "As Object" from line 3.
0
PeterBaileyUkAuthor Commented:
here's the effect
ee.JPG
0
käµfm³d 👽Commented:
What editor are you using?
0
PeterBaileyUkAuthor Commented:
i am in vb access
0
PeterBaileyUkAuthor Commented:
VBscript in regexp buddy
0
käµfm³d 👽Commented:
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:

Screenshot
0
PeterBaileyUkAuthor Commented:
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

0
käµfm³d 👽Commented:
Change lines 7 and 24 to use the updated constant names.
0
PeterBaileyUkAuthor Commented:
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

0
PeterBaileyUkAuthor Commented:
I just spotted that its not complaining now just about to run it
0
PeterBaileyUkAuthor Commented:
its run but says it cannot find the htmlfile2.txt which makes sense as it hasnt been created yet
0
PeterBaileyUkAuthor Commented:
thats got it...THANK YOU
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.