I am successfully replacing a text in a file.
Dim fileReader As String = My.Computer.FileSystem.ReadAllText("C:\test.txt").Replace("foo", "bar")
My.Computer.FileSystem.WriteAllText("C:\test.txt", fileReader, False)
But how can I replace if I don't know the middle of the text ? For example
example_my("browser.taskbar.lastgroupid", "E7CF176E110C211B");
How to replace the E7CF176E110C211B ?
One way is to use RegEx. You can specify the first and last parts and then use \d+ or better [0-9A-F]+ to match any hex number. So given that the part that you don't know is a hexadecimal number, you can use the following code:
Dim fileReader As String = My.Computer.FileSystem.ReadAllText("C:\test.txt")
fileReader = System.Text.RegularExpressions.RegEx.Replace(fileReader, "[0-9A-F]+", "bar")
My.Computer.FileSystem.WriteAllText("C:\test.txt", fileReader, False)
This will replace all hexadecimal numbers in the file with the word "bar".
Note that for a text file this kind of global Replace can have undesirable results. For example the above regex will replace letters A to F even within normal text. So you'll probably want to put some kind of limitations such as min. number of digits to make sure it replaces only valid hex numbers. For example you may want to use [0-9A-F]{4,} which will replace only 4 or more consecutive hex digits. But even that is not 100% safe; e.g. it will replace DEAD, FACE, FADE, FACED etc. with the word "bar". To figure out exactly what is 100% safe for your file, you'll need to examine its contents carefully.
Edit
Reading you comments, you can use the following code instead:
Dim fileReader As String = My.Computer.FileSystem.ReadAllText("C:\test.txt")
fileReader = System.Text.RegularExpressions.RegEx.Replace(fileReader , "lastgroupid"", ""(.+)""\)", "lastgroupid"", ""ANYTHING_YOU_WANT""\)")
My.Computer.FileSystem.WriteAllText("C:\test.txt", fileReader, False)
This will replace all occurrances of hex numbers between lastgroupid", " AND ") with the string ANYTHING_YOU_WANT.
Related
I have searched the whole internet, and the only things I can find is IndexOf. The issue with that is I need a way to put the insert at a specific one of those chars.
I am currently using this
RichTextBox1.Text = RichTextBox1.Text.Insert(RichTextBox1.Text.Substring(0,RichTextBox1.Text.Split("^")(CurrentSlide).Length), "^")
Which of course is completely incorrect after thinking about it, because the length of the index is not the real length of the text to the index.
I think you have the right idea. It seems like you have a "^" delimited string, and you want to insert an empty at some point. Since a collection is easier to work with than a string, your Split is a good start.
Dim parts = RichTextBox1.Text.Split("^")
Let's make that a list, so we can insert into it:
Dim parts = RichTextBox1.Text.Split("^").ToList()
Now, just insert an empty string where you want the new element:
Dim parts = RichTextBox1.Text.Split("^").ToList()
parts.Insert(CurrentSlide, "")
And combine them back for editing in your textbox:
Dim parts = RichTextBox1.Text.Split("^").ToList()
parts.Insert(CurrentSlide, "")
RichTextBox1.Text = String.Join("^", parts)
To start here is an example of a line I am trying to manipulate:
trait slot QName(PrivateNamespace("*", "com.company.assembleegameclient.ui:StatusBar"), "_-0IA") type QName(PackageNamespace(""), "Boolean") value False() end
I wrote a code that will go through and read through each line and stop at the appropriate line. What I am trying to achieve now is to read through the characters and save just the
_-0IA
to a new string. I tried using Trim(), Replace(), and indexof so far but I am having a ton of difficulties because of the quotation marks. Has anyone deal with this issue before?
Assuming your source string will always follow a strict format with only some data changes, something like this might work:
'Split the string by "," and extract the 3rd element. Trim the space and _
quotation mark from the front and extract the first 5 characters.
Dim targetstr As String = sourcestr.Split(","c)(2).TrimStart(" """.ToCharArray).Substring(0, 5)
If the length of the target string is variable it can be done like this:
Dim temp As String = teststr.Split(","c)(2).TrimStart(" """.ToCharArray)
'Use the index of the next quotation mark instead of a fixed length
Dim targetstr As String = temp.Substring(0, temp.IndexOf(""""c))
Hey guys I'm stuck with this question. Please help.
I want to write a program that can extract alphabetical characters and special characters from an input string. An alphabetical character is any character from "a" to "z"(capital letters and numbers not included") a special character is any other character that is not alphanumerical.
Example:
string = hello//this-is-my-string#capetown
alphanumerical characters = hellothisismystringcapetown
special characters = //---#
Now my question is this:
How do I loop through all the characters?
(the for loop I'm using reads like this for x = 0 to strname.length)...is this correct?
How do I extract characters to a string?
How do I determine special characters?
any input is greatly appreciated.
Thank you very much for your time.
You could loop through each character as follows:
For Each _char As Char In strname
'Code here
Next
or
For x as integer = 0 to strname.length - 1
'Code here
Next
or you can use Regex to replace the values you do not need in your string (I think this may be faster but I am no expert) Take a look at: http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx
Edit
The replacement code will look something as follows although I am not so sure what the regular expression (variable called pattern currently only replacing digits) would be:
Dim pattern As String = "(\d+)?" 'You need to update the regular expression here
Dim input As String = "123//hello//this-is-my-string#capetown"
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(input, "")
Since you need to keep the values, you'll want to loop through your string. Keeping a list of characters as a result will come in handy since you can build a fresh string later. Then take advantage of a simple Regex test to determine where to place things. The psuedo code looks something like this.
Dim alphaChars As New List(Of String)
Dim specialChars As New List(Of String)
For Each _char As Char in testString
If Regex.IsMatch(_char, "[a-z]")) Then
alphaChars.Add(_char)
Else
specialChars.Add(_char)
End If
Next
Then If you need to dump your results into a full string, you can simply use
String.Join(String.Empty, alphaChars.ToArray())
Note that this code makes the assumption that ANYTHING else than a-z is considered a special character, so if needs be you can do a second regular expression in your else clause to test for you special characters in a similar manner. It really depends on how much control you have over the input.
Is there a way to detect a Chinese character in a string which is build like this:
dim test as string = "letters 中國的"
Now I want to substring only the Chinese characters. But my code is database driven, so I can't substring it, because the length is always different. So is there a way I can split the string, from the moment I detect a Chinese character?
I think you can use regexp like in the following example, didn't test and I haven't code using VB.net for years So syntax may be not correct.
Dim m As Match = Regex.Match(value, "[\u4e00-\u9fa5]+",
RegexOptions.IgnoreCase)
' If successful, write the group.
If (m.Success) Then
Dim key As String = m.Groups(1).Value
End If
http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx
Let say i have a string like this 'this is a statement'
and if i want to search and replace string with this 'this ** a statement'
string to search for this is a statement , this si a statement , this i a statement and any combination convert them into this trim a statement
i.e for any word combination between this & a statement replace it with trim
for another set replace fun to notfun .
so this is the program
import re
file=open('file','r+')
search=re.sub('this \(a_zA_Z0_9)+ a statement','\1trim',file),('this is fun','this is notfun',file)
file.close()
something is not right as nothing is getting changed in the file.
thanks everyone.
re.sub doesn't work on files, it works on strings. You need to read the contents of the file into a string, then use re.sub to change the string, then write the modified string back to the file.
A simple example:
text = open("myfile.txt").read()
# This is your original re.sub call, but I'm not sure it really does what you want.
text = re.sub('this \(a_zA_Z0_9)+ a statement', '\1trim', text)
text = re.sub('this \(a_zA_Z0_9)+ another replacement', 'some other thing', text)
open("myfile.txt", "w").write(text)