|
-
Jul 10th, 2006, 04:55 PM
#1
Thread Starter
Frenzied Member
Search & delete a string in text File
Hi,
How to search for a string in a text file and delete it.
my search string is:
{*test*}
here * is one or more characters.
Thanks in advance.
-
Jul 10th, 2006, 05:05 PM
#2
Re: Search & delete a string in text File
You'll need to read the content of the file into a string and do the replacement and then write the string back.
To do replacement with wildchars you probably want to use regular expressions.
-
Jul 10th, 2006, 05:14 PM
#3
Re: Search & delete a string in text File
here's a non regexp version:
VB Code:
Dim sText As String, lPos As Long, lStart As Long, lEnd As Long
Open "C:\file.txt" For Input As #1
sText = Input(LOF(1), #1)
Close #1
lStart = 1
Do
lPos = InStr(lStart, sText, "test")
If lPos Then
lStart = InStrRev(sText, "{", lPos)
lEnd = InStr(lPos, sText, "}")
If lEnd > 0 And lStart > 0 Then
sText = Left$(sText, lStart - 1) & Mid$(sText, lEnd + 1)
Else
lStart = lPos + 4
End If
End If
Loop Until lPos = 0
Open "C:\file.txt" For Output As #1
Print #1, sText
Close #1
-
Jul 10th, 2006, 05:30 PM
#4
Thread Starter
Frenzied Member
Re: Search & delete a string in text File
Thanks Bush.
Is it faster and better way than regexp version?
Can you post the regexp version also please?
-
Jul 10th, 2006, 05:37 PM
#5
Re: Search & delete a string in text File
I don't know any regexp so I can't help you there.
I also couldn't comment on the speed - if you want to do it really fast then you probably wouldn't use my code or regexp. The one thing I can say for certain is that the RegExp method will use a lot more memory.
-
Jul 10th, 2006, 05:45 PM
#6
Thread Starter
Frenzied Member
Re: Search & delete a string in text File
No problemo Bush. Your code helped me a lot. Thanks.
I will wait for someone else to get some help on regexp version also.
Thanks a lot.
-
Jul 10th, 2006, 07:39 PM
#7
Re: Search & delete a string in text File
The point being that you could you use RegEx if you wanted to use wildcards, and since you stated you can use BM's example,
then RegEx may not be required.
Additionaly, why can't you use Replace() like Joacim mentioned??
Load the file into a buffer, then Replace(), and save back out.
-
Jul 10th, 2006, 07:50 PM
#8
Thread Starter
Frenzied Member
Re: Search & delete a string in text File
 Originally Posted by Bruce Fox
why can't you use Replace() like Joacim mentioned??
Load the file into a buffer, then Replace(), and save back out.
the search string is not constant. As mentioned in my post #1, I want to search *test* where * may be one or more characters. so i cannot use replace().
 Originally Posted by Bruce Fox
The point being that you could you use RegEx if you wanted to use wildcards, and since you stated you can use BM's example, then RegEx may not be required.
Currently I use BM's code. I am also looking for code which uses Reg Expressions. so that I can learn something about RegExp.
Thanks.
-
Jul 10th, 2006, 08:22 PM
#9
Re: Search & delete a string in text File
In the example you provided (and as in BM's example) you can of course use Replace()!
You can create a Sub and pass the search string to it.
-
Jul 10th, 2006, 09:25 PM
#10
Re: Search & delete a string in text File
I never suggested that the Replace function should be used since it doesn't support wildchars. What I said was simply to do the replacement using regexp.
Here's an example, it requires a reference to "Microsoft VBScript Regular Expressions"
VB Code:
Dim rx As RegExp
Dim sText As String
'Code to read the content of the file and store it
'in sText goes here...
Set rx = New RegExp
rx.Global = True 'replace all, set this to false to only replace the first match
rx.IgnoreCase = True 'case insensitive search
rx.Pattern = "({[\s\S]*?)test([\s\S]*?})"
sText = rx.Replace(sText, "$1$2")
'code to write back the content of sText to the
'file goes here
The above will remove the word "test" if it is somewhere inside curly brackets (or did you want to remove everything inside the brackets if the word "test" was found?).
-
Jul 11th, 2006, 05:54 AM
#11
Thread Starter
Frenzied Member
Re: Search & delete a string in text File
Thanks Joacim.
 Originally Posted by Joacim Andersson
did you want to remove everything inside the brackets if the word "test" was found?.
Yes. I want to remove the entire search string including curly braces.
The conditions are:
1. The string should be withing curly braces.
2. The string should have the word "test"
I want to remove the entire string including curly braces, if it satisfies the above criterias. For example,
Code:
{/a \d test jdd} - This should be removed. it satisfies the criterias
{/a \d tost jdd} - This should not be removed. it doesn't satisfies the criterias
What are the advantages and disadvantages of using RegExp?
Thanks.
Last edited by cssriraman; Jul 11th, 2006 at 06:02 AM.
CS
-
Jul 11th, 2006, 04:54 PM
#12
Re: Search & delete a string in text File
OK, in that case simply change the last two lines to:
VB Code:
rx.Pattern = "{[\s\S]*?test[\s\S]*?}"
sText = rx.Replace(sText, "")
That will remove it all if the word "test" is somewhere inside the curly brackets (actually it will remove it if the word "testing" or something like that exists. Let me know if you need an exact word match).
The main advantage of using regular expression is that you with a very few lines can search for (and replace) text with a complex pattern matching.
The main disadvantage is that regex is a bit cryptic to say the least, even for a regex pro it takes a while to read and understand what it does. (Just make sure you write clear comments and you'll overcome that).
Another advantage is that, in most cases, it runs much faster than using VBs own string manipulation functions.
There are other disadvantages related to the MS implementation of regular expressions also since it doesn't cover all of them and even some documented wildcard characters doesn't work as expected. You also need another reference, but you're not allowed to distribute the file. People need to have it installed already (or download and install it from MS). But that's not a big problem since everyone that has MSH installed has it already and that comes with Windows, at least since Win98SE.
I don't know if it uses more memory as Bush claimed. I actually don't think it does except for the fact that you need to create a new object which of course consumes a bit of memory.
I personally found regexp to be invalueble when it comes to string parsing, especially when parsing larger text. Once you've learned how to use it I'm sure you will find it that way as well.
-
Jul 11th, 2006, 05:09 PM
#13
Thread Starter
Frenzied Member
Re: Search & delete a string in text File
Thanks a lot for the information.
I will check this and come back.
Thanks.
Last edited by cssriraman; Jul 11th, 2006 at 05:15 PM.
CS
-
Jul 11th, 2006, 05:17 PM
#14
Re: Search & delete a string in text File
That sounds strange. I did a test for a text typed on several lines in a multiline textbox and that worked just fine. Which version of the MS VBScript Regular Expression library are you using?
EDIT: Ehhh... Did you just change your previous post?
-
Jul 11th, 2006, 05:57 PM
#15
Thread Starter
Frenzied Member
Re: Search & delete a string in text File
I'm sorry. I got different outputs. So, I want to check again. I will come back to you, once I checked. That's why I edited my previous post.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|