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.
Printable View
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.
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.
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
Thanks Bush.
Is it faster and better way than regexp version?
Can you post the regexp version also please?
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.
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.
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.
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().Quote:
Originally Posted by Bruce Fox
Currently I use BM's code. I am also looking for code which uses Reg Expressions. so that I can learn something about RegExp.Quote:
Originally Posted by Bruce Fox
Thanks.
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.
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"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?).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
Thanks Joacim.Yes. I want to remove the entire search string including curly braces.Quote:
Originally Posted by Joacim Andersson
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,What are the advantages and disadvantages of using RegExp?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
Thanks.
OK, in that case simply change the last two lines to: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).VB Code:
rx.Pattern = "{[\s\S]*?test[\s\S]*?}" sText = rx.Replace(sText, "")
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.
Thanks a lot for the information.
I will check this and come back.
Thanks.
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?
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.