How to remove double spaces in vb.net - regex

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, " ")

Related

Exclude some words from regular expression

I have function which inserts space after characters like : / -
Private Function formatColon(oldString As String) As String
Dim reg As New RegExp: reg.Global = True: reg.Pattern = "(\D:|\D/|\D-)"
Dim newString As String: newString = reg.Replace(oldString, "$1 ")
formatColon = Replace(Replace(Replace(newString, ": ", ": "), "/ ", "/ "), "- ", "- ")
End Function
The code excludes dates easily. I want to exclude some a particular strings like 'w/d' also.
Is there any way?
before abc/abc/15/06/2017 ref:123243-11 ref-111 w/d
after abc/ abc/ 15/06/2017 ref: 123243-11 ref- 111 w/ d
i want to exclude last w/d
You may use a (?!w/d) lookahead to avoid matching w/d with your pattern:
Dim oldString As String, newString As String
Dim reg As New RegExp
With reg
.Global = True
.Pattern = "(?!w/d)\D[:/-]"
End With
oldString = "abc/abc/15/06/2017 ref:123243-11 ref-111 w/d"
newString = reg.Replace(oldString, "$& ")
Debug.Print newString
See the regex demo.
Pattern details
(?!w/d) - the location not followed with w/d
\D - any non-digit char
[:/-] - a :, / or - char.
The $& backreference refers to the whole match from the replacement pattern, no need to enclose the whole pattern with the capturing parentheses.
Here is another solution.
^/(?!ignoreme$)(?!ignoreme2$)[a-z0-9]+$

Replace part of String, using regex and vb.net

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

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)

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.

Visual Basic 2013 RegEx

How can I get only the Name?
"Hello Name!"
Dim r As New Regex("Hello (.*)! :)")
Dim matches As MatchCollection = r.Matches(Chat)
For Each m As Match In matches
MsgBox("Hi " & m.ToString & " and welcome back!")
Next
(Chat is the last Chatmessage)
To get only name, you should use Groups. And if you really want to match :), you must escape ) by using \:
Dim Chat As String = "Hello Name!"
Dim r As New Regex("Hello (.*)! :\)")
Dim matches As MatchCollection = r.Matches(Chat)
For Each m As Match In matches
m.Groups(1).Value
Next
Why don't you do a string replace for Hello and the ! as you know these are fixed??
I don't know VB, but a REGEX in other language should be Hello (\S+)!