In a string that has multiple double-quotes, is there an easier solution than escaping each and everyone of them?
For instance, here's an HTML string:
Dim test As Regex = New Regex("^<div class="blah">\r\n<div class="blah"></div>\r\n</div>\r\n<div class="blah">\r\n(.+?)\r\n<div> class="blah">\r\n", RegexOptions.Singleline)
Alternatively, to hold the string, can VB.Net be told to use another character than the double-quote, eg.
New Regex(#my string="" my other string=""#)
?
Thank you.
If you are certain that the # symbol does not appear in your string...
Dim s As String = Replace("^<div class=#blah#>\r\n<div class=#blah#></div>\r\n</div>\r\n<div class=#blah#>\r\n(.+?)\r\n<div> class=#blah#>\r\n", "#", """")
Dim test As Regex = New Regex(s, RegexOptions.Singleline)
Related
I have many strings that have numbers at the end. The numbers can be of any size, for example:
myvar123
mysecondvar3
mythirdvar219107
The strings can have numbers even inside the name, not only at the end.
for example:
my2varable123
some123variable9480395
I would need to replace any number at the END with a placeholder. (NOT those inside the varname.)
for example:
my2varable123 should become: my2variable%placeholder%
some123variable9480395 should become: some123variable%placeholder%
The only way that comes to my mind is to go through the string using .right() and remove the char if it is numeric until I find the first non numeric char. Then in the end append the placeholder, but it looks like a lot of work for a rather simply problem.
Is there a better way to do this?
You could use a Regex for this.
Dim str As String = "some123variable9480395"
Dim pattern As String = "\d+$"
Dim replacement As String = "%placeholder%"
Dim rgx As Regex = New Regex(pattern)
Dim result As String = rgx.Replace(str, replacement)
Let me say, I hate working with strings! I'm trying to find a way to split a string on brackets. For example, the string is:
Hello (this is) me!
And, from this string, get an array with Hello and me. I would like to do this with parentheses and braces (not with brackets). Please note that the string is variable, so something like SubString wouldn't work.
Thanks in advance,
FWhite
You can use regular expressions (Regex), below code should exclude text inside all parenthesis and braces, also removes an exclamation mark - feel free to expand CleanUp method to filter out other punctuation symbols:
Imports System.Text.RegularExpressions
Module Module1
Sub Main()
Dim re As New Regex("\(.*\)|{.*}") 'anything inside parenthesis OR braces
Dim input As String = "Hello (this is) me and {that is} him!"
Dim inputParsed As String = re.Replace(input, String.Empty)
Dim reSplit As New Regex("\b") 'split by word boundary
Dim output() As String = CleanUp(reSplit.Split(inputParsed))
'output = {"Hello", "me", "and", "him"}
End Sub
Private Function CleanUp(output As String()) As String()
Dim outputFiltered As New List(Of String)
For Each v As String In output
If String.IsNullOrWhiteSpace(v) Then Continue For 'remove spaces
If v = "!" Then Continue For 'remove punctuation, feel free to expand
outputFiltered.Add(v)
Next
Return outputFiltered.ToArray
End Function
End Module
To explain the regular expression I used (\(.*\)|{.*}):
\( is just a (, parenthesis is a special symbol in Regex, needs to be escaped with a \.
.* means anything, i.e. literally any combination of characters.
| is a logical OR, so the expression will match either left or ride side of it.
{ does not need escaping, so it just goes as is.
Overall, you can read this as Find anything inside parenthesis or braces, then the code says replace the findings with an empty string, i.e. remove all occurrences. One of the interesting concepts here is understanding greedy vs lazy matching. In this particular case greedy (default) works well, but it's good to know other options.
Useful resources for working with Regex:
http://regex101.com/ - Regex test/practice/sandbox.
http://www.regular-expressions.info/ - Theory and examples.
http://www.regular-expressions.info/wordboundaries.html - How word boundaries work.
Try this code:
Dim var As String = "Hello ( me!"
Dim arr() As String = var.Split("(")
MsgBox(arr(0)) 'Display Hello
MsgBox(arr(1)) 'Display me!
Something like this should work for you:
Dim x As String = "Hello (this is) me"
Dim firstString As String = x.Substring(0, x.IndexOf("("))
Dim secondString As String = x.Substring(x.IndexOf(")") + 1)
Dim finalString = firstString & secondString
x = "Hello (this is) me"
firstString = "Hello "
secondString = " me"
finalString = "Hello me"
I am looking for a way to remove characters from any string that are not alphabetical characters.
I am basically just using Replace for every non-Alphabetical character. This method would take forever.
I guess I could make an array (I think) but that would still take quite a while. Is there any simple solution?
Dim wordy As String = textBox.Text.ToUpper.Replace(".", "").Replace("!", "").Replace(" ", "").Replace("'", "").Replace("?", "") _
.Replace(",", "").Replace("-", "")
The following lines of code should help.
MsgBox(Regex.Replace(s, "[^a-zA-Z ]", ""))
This will keep only upper/lowercase A-Z as well as spaces.
Your example,
Dim wordy As String = textBox.Text.ToUpper.Regex.Replace(s, "[^a-zA-Z ]", "")
You could also just use a MaskedTextBox that would allow only numeric input based on the mask.
This will remove all characters except A-Z in lower and upper case, as well as spaces. If you want spaces to be removed, remove the space from the end of the regular expression.
Dim rgx As New Regex("[^a-zA-Z ]")
Dim wordy As String = rgx.Replace(textBox.Text,"")
I have to following string:
Dim text As String = "userĖ˛upload"
I want to change the Unicode character #0332 to underscore. I have the following code for this:
Dim test As New Text.StringBuilder
test.Append(text.Replace("#0332", "_"))
Dim normalizedUrl As String = test.ToString()
However it does not work, my "test" string has the same value as "text" variable. Anyone has an idea what can be wrong?
You can;
text.Replace(ChrW(&H332), "_")
Funny, I had a textbox and I could append strings to it.
But now I create a string like this:
Dim str As String = New String("")
And I want to append to it other strings. But there is no function for doing so. What am I doing wrong?
Concatenate with & operator
Dim str as String 'no need to create a string instance
str = "Hello " & "World"
You can concate with the + operator as well but you can get yourself into trouble when trying to concatenate numbers.
Concatenate with String.Concat()
str = String.Concat("Hello ", "World")
Useful when concatenating array of strings
StringBuilder.Append()
When concatenating large amounts of strings use StringBuilder, it will result in much better performance.
Dim sb as new System.Text.StringBuilder()
str = sb.Append("Hello").Append(" ").Append("World").ToString()
Strings in .NET are immutable, resulting in a new String object being instantiated for every concatenation as well a garbage collection thereof.
Another way to do this is to add the new characters to the string as follows:
Dim str As String
str = ""
To append text to your string this way:
str = str & "and this is more text"
Use the string concatenation operator:
Dim str As String = New String("") & "some other string"
Strings in .NET are immutable and thus there exist no concept of appending strings. All string modifications causes a new string to be created and returned.
This obviously cause a terrible performance. In common everyday code this isn't an issue, but if you're doing intensive string operations in which time is of the essence then you will benefit from looking into the StringBuilder class. It allow you to queue appends. Once you're done appending you can ask it to actually perform all the queued operations.
See "How to: Concatenate Multiple Strings" for more information on both methods.