Replace part of String, using regex and vb.net - regex

My application stores the path of the userdata.dll in a String.
I need to convert this string: C:\Applications\User\userdata.dll
into this: C:\\Applications\\User\\userdata.dll
All \ will need to be duplicated, independent on how many \ the path have.
Something like:
Dim defaultPath As String = "C:\Applications\User\userdata.dll"
' Regex
Dim r As Regex = New Regex( ... )
' This is the replacement string
Dim Replacement As String = " ... $1 ... "
' Replace the matched text in the InputText using the replacement pattern
Dim modifiedPath As String = r.Replace(defaultPath,Replacement)
Any help on this? I am trying to follow this question:
How to replace some part of this string with vb.net?
But cant find out how to make this Regex...

You can use
Dim pattern As String = "\\"
Dim rgx As New Regex(pattern)
Dim input As String = "C:\Applications\User\userdata.dll"
Dim result As String = rgx.Replace(input, "\\")
Console.WriteLine(result)
Ideone Demo
If you mean to say that replace any number of \ to \\, then you can use
Dim pattern As String = "\\+"
Dim rgx As New Regex(pattern)
Dim input As String = "C:\\\\Applications\User\userdata.dll"
Dim result As String = rgx.Replace(input, "\\")
Ideone Demo

Related

Match and replace first and last character and substring in string using regex VB NET

Sorry for my simple question but I don't know how to do.
I have this string:
Dim SourceString = "{capital?} has a bridge for {people?}"
Now I want ResultString like this:
ResultString = "capital_Den has a bridge for people_Den"
I used
Dim str As String = "{capital?} has a bridge for {people?}"
Dim str1 As String str1 = Regex.Replace(str, "\{?\?\}", "_DEN}")
Result: {capital_DEN} has a bridge for {people_DEN}
But I want this result: capital_DEN has a bridge for people_DEN
The \{?\?\} pattern matches an optional {, a ? and then a } char.
You may use
str1 = Regex.Replace(str, "\{(\w+)\?\}", "$1_DEN")
Or, if there can be more than just word chars inside:
str1 = Regex.Replace(str, "\{([^{}]+)\?\}", "$1_DEN")
See the VB.NET demo online and the regex demo. The pattern matches:
\{ - a { char
(\w+) - Group 1: one or more word chars
[^{}]+ - 1+ chars other than { and }
\?\} - a ?} substring.
Full VB.NET code snippet:
Dim str As String = "{capital?} has a bridge for {people?}"
Dim str1 As String
str1 = Regex.Replace(str, "\{(\w+)\?\}", "$1_DEN")
Console.WriteLine(str1)
' -> capital_DEN has a bridge for people_DEN
First Make a ConsoleApplication
Module Module1
Sub Main()
Console.Title = "Combine"
Dim a As String = "capital_Den"
Dim b As String = "people_Mar"
Dim ResultString As String = a & " has a bridge for " & b
Console.ForegroundColor = ConsoleColor.Green
Console.WriteLine(ResultString)
Console.ReadKey()
End Sub
End Module

How to overcome Replace String Error with RegEx Replacement

Below is my code
Dim ATT As String = "Hi Sample $1$ Text"
Dim newText As String = "<ss>Hello How Are you</ss>"
newText = Regex.Replace(newText, "<ss>(.*?)</ss>", ATT, RegexOptions.IgnoreCase)
The above Code Treats the string content ("$1$") in ATT variable as Regex value and replaces it in the newText variable, how to overcome this, please guide,
Is there any thing available like we have in perl mentioned below to overcome this problem,
$_=~ s/<ss>(.*?)</ss>/\Q$ATT\E/g;
You can either escape the $ characters in the replacement string:
Dim ATT As String = "Hi Sample $1$ Text"
ATT = ATT.Replace("$", "$$")
Dim newText As String = "<ss>Hello How Are you</ss>"
newText = Regex.Replace(newText, "<ss>(.*?)</ss>", ATT, RegexOptions.IgnoreCase)
Or use a lambda for the replacement:
Dim ATT As String = "Hi Sample $1$ Text"
Dim newText As String = "<ss>Hello How Are you</ss>"
newText = Regex.Replace(newText, "<ss>(.*?)</ss>", Function(m) ATT, RegexOptions.IgnoreCase)

Replace the second char of a string

I have a string variable.
Dim str As String = "ABBCD"
I want to replace only the second 'B' character of str (I mean the second occurrence)
my code
Dim regex As New Regex("B")
Dim result As String = regex.Replace(str, "x", 2)
'result: AxxCD
'but I want: ABxCD
What's the easiest way to do this with Regular Expressions.
thanks
Dim str As String = "ABBCD"
Dim matches As MatchCollection = Regex.Matches(str, "B")
If matches.Count >= 2 Then
str = str.Remove(matches(1).Index, matches(1).Length)
str = str.Insert(matches(1).Index, "x")
End If
First we declare the string 'str', then find the matches of "B". If we found two results or more, replace the second result with "x".
How about:
resultString = Regex.Replace(subjectString, #"(B)\1", "$+x");
Use a positive lookbehind:
Dim regex As New Regex("(?<=B)B")
Live demo
If ABCABCABC should produce ABCAxCABC, then the following regex will work:
(?<=^[^B]*B[^B]*)B
Usage:
Dim result As String = Regex.Replace(str, "(?<=^[^B]*B[^B]*)B", "x")
I assume BB was just an example, it can be CC, DD, EE, etc..
Based on that, the regex below will replace any repeated character in the string.
resultString = Regex.Replace(subjectString, #"(\w)\1", "$1x");
'Alternative way to replace the second occurrence
'only of B in the string with X
Dim str As String = "ABBCD"
Dim pattern As String = "B"
Dim reg As Regex = New Regex(pattern)
Dim replacement As String = "X"
'find position of second B
Dim secondBpos As Integer = Regex.Matches(str, pattern)(1).Index
'replace that B with X
Dim result As String = reg.Replace(str, replacement, 1, secondBpos)
MessageBox.Show(result)

Remove all the String before :

"\:(.*)$"
Hi all i am using above expression to remove all the string before : (colon), but it is giving me all the string before this. how can i do this. Thanks a lot.
My string is:
This is text: Hi here we go
I am getting: This is text
I want : Hi here we go
Updated code
Sub Main()
Dim input As String = "This is text with : far too much "
Dim pattern As String = "\:(.*)$"
Dim replacement As String = " "
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(input, replacement)
Console.WriteLine("Original String: {0}", input)
' MsgBox("Original String: {0}")
Console.WriteLine("Replacement String: {0}", result)
MsgBox("Original String: {0}")
End Sub
Try this pattern. This will help you to match string after colon
/?:(.)/
or
/: (.+)/
It should be:
Dim pattern As String = "(.*)\:"
' in vb if above one doesn't work, then try this one
' Dim pattern As String = "^(.*)\:"
' also i don't think we need to use any brackets here as well.
This regex means, anything before the colon(:), Where you were using anything after the colon(:) in your example.
If you are not dead set on RegEx then you can also use
Dim result As String
result = Strings.Split(Input, ":", 2)(1)
This splits the input into an array with two elements. First element is the text before the first ":", the second element is the text after.

Regular expression to extract numbers from long string containing lots of punctuation

I am trying to separate numbers from a string which includes %,/,etc for eg (%2459348?:, or :2434545/%). How can I separate it, in VB.net
you want only the numbers right?
then you could do it like this
Dim theString As String = "/79465*44498%464"
Dim ret = Regex.Replace(theString, "[^0-9]", String.Empty)
hth
edit:
or do you want to split by all non number chars?
then it would go like this
Dim ret = Regex.Split(theString, "[^0-9]")
You could loop through each character of the string and check the .IsNumber() on it.
This should do:
Dim test As String = "%2459348?:"
Dim match As Match = Regex.Match(test, "\d+")
If match.Success Then
Dim result As String = match.Value
' Do something with result
End If
Result = 2459348
Here's a function which will extract all of the numbers out of a string.
Public Function GetNumbers(ByVal str as String) As String
Dim builder As New StringBuilder()
For Each c in str
If Char.IsNumber(c) Then
builder.Append(c)
End If
Next
return builder.ToString()
End Function