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)
Related
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
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)
"\:(.*)$"
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.
It's the same with How to remove double white space character using regexp?
but for vb.net
how to do so?
Dim text = "My name is Anas Fares"
Dim newText = System.Text.RegularExpressions.Regex.Replace(text, "[ ]{2,}", " ")
Or this one if you want to get rid of TABS, NEWLINES etc etc etc.
Dim text = "My name is " & Environment.NewLine & " Anas Fares"
Dim newText = System.Text.RegularExpressions.Regex.Replace(text, "\s+", " ")
You would simply do a string replace on a double white space, to a single white space:
Dim str = "Lorem Ipsum Dolar"
str = str.replace(" ", " ")
return str 'Lorem Ipsum Dolar
But you could still use the same regular expression in VB.NET as used in the javascript code from your link. This article shows a way of doing just that:
http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx
Dim input As String = "This is text with far too much " + _
"whitespace."
Dim pattern As String = "\s+"
Dim replacement As String = " "
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(input, replacement)
Console.WriteLine("Original String: {0}", input)
Console.WriteLine("Replacement String: {0}", result)
A direct conversion would be:
Dim r As Regex = New Regex("\s{2,}") 'or " {2,}" or "[ ]{2,}", depending on if you want whitespace, or just spaces.
Dim stringWithDoubleSpaces As String = "this is a double spaced string"
Dim stringWithSingleSpaces As String = r.Replace(stringWithDoubleSpaces, " ")
How to extract text using regex in vb6
Dim MyText As String
MyText = "anything [*]textToExtract[*] anything"
Result Should Be :
textToExtract
Sub test()
Dim re As RegExp, m As MatchCollection
Set re = New RegExp
Dim MyText As String, extractedText As String
MyText = "anything textToExtract anything"
re.Pattern = "anything (.*) anything"
Set m = re.Execute(MyText)
extractedText = m(0).SubMatches(0)
End Sub