VB.net regex exclude certain characters from string - vb.net

I am using regex.ismatch to check a string doesn't contain any one of a list of characters such as £&+(/?!;:* And also a quotation mark " not sure how to place that...
But can't get to to work...
If Regex.ismatch(Line, "^[^##£&+()*']"). Then
Msgbox("error")
End If
But doesn't work for me?
Any suggestions

You could do this pretty easily without Regex by simply doing something like this:
Public Shared Function HasSpecialChars(ByVal str As String) As Boolean
Const specialChars As String = "!##$%^&*()"
Dim indexOf As Integer = str.IndexOfAny(specialChars.ToCharArray())
Return indexOf <> -1
End Function

Related

How can I convert a string into different characters?

I have looked on the web and I cannot find anything that helps me, all I can find is changing the characters into ASCII or Hexadecimal. However I would like to do it a different way. For example, say the string that got passed in was abcdef, I would like to have a key which changes these characters into another string such as qwpolz. Is there an easier way than declaring each character in the alphabet to be another character like:
Dim sText As String = "Hello"
Dim sEncode As String = ""
Dim iLength As Integer
Dim i As Integer
iLength = Len(sText)
For i = 1 To iLength
sEncode = sEncode ????
Next
Return sEncode
And then have a very lengthy loop which checks for these loops? There must be a much simpler way. Can anybody help by pointing me in the right direction?
Edit: Why downvote? Seriously, it's a legitimate question. Instead of downvoting for no reason, just move onto another question.
Well actually, this sounds like a Caesar sipher
Private Overloads Shared Function Encrypt(ByVal ch As Char, ByVal code As Integer) As Char
If Not Char.IsLetter(ch) Then
Return ch
End If
Dim offset As Char = IIf(Char.IsUpper(ch), "A", "a")
Return CType((((ch + (code - offset)) Mod 26) + offset),Char)
End Function
Private Overloads Shared Function Encrypt(ByVal input As String, ByVal code As Integer) As String
Return New String(input.ToCharArray.Select(() => { }, Encrypt(ch, code)).ToArray)
End Function
Private Shared Function Decrypt(ByVal input As String, ByVal code As Integer) As String
Return Encrypt(input, (26 - code))
End Function
Note that this assumes, that you use English alphabet. In general case where for example you have 'ä', 'ö', 'š', 'ž', 'ß', 'õ', 'ü' etc. this would not work. In that case it is simpler to just create a list/dictionary of your ordered alphabet and use it.
Example use:
encrypted = Encrypt(sText, 5)
decypted = Decrypt(encrypted, 5)
Sounds as if you want to modify a string by replacing each character with a different character according to a mapping table. An efficient approach is to use a Dictionary(Of Char, Char). But easier to write and maintain is something like this:
Shared ReadOnly replaceChars As String = "abcdef"
Shared ReadOnly withChars As String = "qwpolz"
Public Shared Function ReplaceAll(input As String) As String
Dim newChars = From c In input
Let index = replaceChars.IndexOf(c)
Select If(index >= 0, withChars(index), c)
Return String.Concat(newChars)
End Function
So the first string contains the chars that you want to replace and the second the replacement characters. Both strings must have the same length.
If you want to support case insensitivity:
Public Shared Function ReplaceAll(input As String, comparison As StringComparison) As String
Dim newChars = From c In input
Let index = replaceChars.IndexOf(c.ToString(), comparison)
Select If(index >= 0, withChars(index), c)
Return String.Concat(newChars)
End Function
Note that this is also a loop. There is no way to avoid some kind of loops if you want to replace multiple characters or strings.

Converting characters from Lower to upper case and vice versa VB.Net

had a search around and can't find an answer.
I've been tasked with converting a strings capitalization from whatever it is in Be it lower case or upper case and swap them round..
For Example :- Input :- "HeLlO" and Output :- "hElLo"
I understand that i need to use a for loop but have not been able to figure out how to step through each character, check the case and switch it if needs be.
I can make a for loop that counts through and displays the individual characters or a simple If statement to convert the whole string into Upper or lower but if i try to combine the 2 my logic isn't working right.
Can anyone help at all?
Here is one simple way to do it:
Public Function InvertCase(input As String) As String
Dim output As New StringBuilder()
For Each i As Char In input
If Char.IsLower(i) Then
output.Append(Char.ToUpper(i))
ElseIf Char.IsUpper(i) Then
output.Append(Char.ToLower(i))
Else
output.Append(i)
End If
Next
Return output.ToString()
End Function
It just loops through each character in the original string, checks to see what case it is, fixes it, and then appends that fixed character to a new string (via a StringBuilder object).
As Neolisk suggested in the comments below, you could make it cleaner by creating another method which converts a single character, like this:
Public Function InvertCase(input As Char) As Char
If Char.IsLower(input) Then Return Char.ToUpper(input)
If Char.IsUpper(input) Then Return Char.ToLower(input)
Return input
End Function
Public Function InvertCase(input As String) As String
Dim output As New StringBuilder()
For Each i As Char In input
output.Append(InvertCase(i))
Next
Return output.ToString()
End Function
Using that same function for InvertCase(Char), you could also use LINQ, like this:
Public Function InvertCase(input As String) As String
Return New String(input.Select(Function(i) InvertCase(i)).ToArray())
End Function
As a Linq query:
Dim input = "HeLlO"
Dim output = new String(input.Select(Function(c)
Return If(Char.IsLower(c),Char.ToUpper(c),Char.ToLower(c))
End Function).ToArray())
Console.WriteLine(output)
Honestly, who writes loops these days? :-)

Find space in string, remove everything after it

Hoping this would be simple, but doesnt seem to be.
I've got a variable in vb.net 'contactname'.
format is like "John Smith"
I want to get just the forename from this, but cant seem to do it.
I've found and adapted some examples from google, but nothing seems to work :(
Just Split the string on spaces and take the first element:
contactname.Split(" "c)(0)
Could use Regex if you like:
Public Shared Function RegexGetForename(ByVal str As String) As String
Dim a = New System.Text.RegularExpressions.Regex("^(\w+)")
If a.IsMatch(str) Then
Return a.Match(str).Value
Else
Return vbNull
End If
End Function
Dim forename as string
Dim i = contactname.IndexOf(" ")
If i <> -1 Then
forename = contactname.Substring(0, i)
MsgBox(forename)
End If
try it

String.Split Method optimize way in vb.net

I have a string xxx1-0.1/1 yyy,ccc1,1. I used split and substring. All i want to get is the 0.1/1 only. Is there any optimize way to do this?
Thank you in advance!
Use more than one string split chars. then if the position and format is always the same then this will work.
Dim s As String = "xxx1-0.1/1 yyy,ccc1,1"
Dim ans = s.Split(New Char() {"-"c, " "c})(1)
MessageBox.Show(ans)
Lets make it a function:
Private Function getMySpecialValue(input As String) As String
Return input.Split(New Char() {"-"c, " "c})(1)
End Function

Remove the character alone from the string

My code retrieves the data from various resources .
And output will be like below
UNY4/4/2010
hds04/5/2010
saths04/22/2013
But I want the output like this
4/4/2010
4/5/2010
04/22/2013
Is there any way to do this ?
You need to use a regular expression that finds all uppercase and lowercase characters and replaces them with a blank, like this:
Dim rgx As New Regex("[a-zA-Z]")
str = rgx.Replace(str, String.Empty)
An alternate solution is to look for the first numeric digit, then discard all text before that.
Function GetDate(data As String) As Date
Dim indexFirstNum As Integer = data.IndexOfAny("0123456789".ToCharArray())
Dim datePortion As String = data.Substring(indexFirstNum)
Return Date.Parse(datePortion)
End Function