Find all words in a string that begin with specific characters

lzoumas

New member
Joined
Oct 26, 2011
Messages
1
Programming Experience
10+
Hello,

I have an HTML string and in that string are certain variables that I need to replace with database values. The variable names are dynamic but all begin with the characters "@@" (just two ampersands, no quotes). Is there a way to get all the words in a string that start with "@@"? I'm assuming you would use regular expressions, but not sure how to begin.

Thanks in advance,

Lee
 
First thing I would do is separate the words into an array and then go through that array asking if the words start with @@

here is an example:

Dim str As String Dim aux As String()
Dim res As String()
Dim i As Integer
Dim j As Integer = 0


str = "@@a @@b notThisOne notThisOneEither @@c nop @@d @@end noMore "


aux = str.Split(" ")


For i = 0 To aux.Length - 1
If aux(i).StartsWith("@@") Then
ReDim Preserve res(j)
res(j) = aux(i).Substring(2)
j += 1
End If
Next
 
Dim regEx As New Text.RegularExpressions.Regex("@@[a-zA-Z*]+ ")
Dim sampleHTMLString As String = "@@a @@b notThisOne notThisOneEither @@c nop @@d @@end noMore "
Dim replacement As String = "ReplacementWord" + " "

Console.WriteLine(regEx.Replace(sampleHTMLString, replacement))
---------------------------------------------------------------------------------------------------------------------------------

Dim regEx As New Text.RegularExpressions.Regex("@@[a-zA-Z*]+")
Dim sampleHTMLString As String = "@@a @@b notThisOne notThisOneEither @@c nop @@d @@end noMore "

For Each word As String In sampleHTMLString.Split(" "c)

If regEx.Match(word).Success Then
Console.WriteLine(word)
End If
Next

Hopefully this codes helps....
 
Last edited:
Back
Top