1 Replies - 1543 Views - Last Post: 29 March 2012 - 12:02 AM Rate Topic: -----

#1 Zer0  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-March 12

Finding/replacing multiple sets of string within a parent string

Posted 28 March 2012 - 11:23 PM

Hello everyone,

I've got an issue with a program I am writing. The specific issue is replacing characters in a parent string by finding, and matching, specific characters within it that are parsed from an external xml file. It's for a chat-like client.

Here's the 2 classes I am using to pull the emote strings, titles, and id from external XML:

Imports System.Text.RegularExpressions
Imports System.Xml.Serialization

Public Class emote
    Private strFname As String
    Private strShortcut As String()
    Private emoteRegex As Regex
    Private strTitle As String

    Public Property EmoteId() As String
        Get
            Return strFname
        End Get
        Set(ByVal value As String)
            strFname = value
        End Set
    End Property

    Public Property Shortcuts() As String()
        Get
            Return Me.strShortcut
        End Get
        Set(ByVal value As String())
            Me.strShortcut = value
            If value.Length > 1 Then
                Dim pattern As String = "^("
                For Each str2 As String In value
                    pattern = pattern & Regex.Escape(str2) & "|"
                Next
                pattern = pattern.Remove(pattern.Length - 1) & ")"
                emoteRegex = New Regex(pattern, RegexOptions.IgnoreCase)
            Else
                emoteRegex = New Regex("^" & Regex.Escape(value(0)), RegexOptions.IgnoreCase)
            End If
        End Set
    End Property

    <XmlIgnore()> _
    Public ReadOnly Property SmileyRegex() As Regex
        Get
            Return emoteRegex
        End Get
    End Property

    Public Property Title() As String
        Get
            Return strTitle
        End Get
        Set(ByVal value As String)
            strTitle = value
        End Set
    End Property
End Class


Imports System.Xml.Serialization
Imports System.IO

Public Class clsEmotes
    Private Shared lstSmilies As List(Of emote) = LoadSmilies()

    Private Shared Function LoadSmilies() As List(Of emote)
        Try
            Dim path As String = Application.StartupPath & "\Resources\Emotes\Emotes.xml"
            If File.Exists(path) Then
                Dim serializer As New XmlSerializer(GetType(List(Of emote)))
                Dim stream As New FileStream(path, FileMode.Open, FileAccess.Read)
                Dim list As List(Of emote) = DirectCast(serializer.Deserialize(stream), List(Of emote))
                stream.Close()
                stream.Dispose()
                Return list
            End If
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.WriteLine(ex.InnerException)
        End Try
        Return New List(Of emote)()
    End Function

    Public Shared Sub Save(ByVal list As List(Of emote))
        Dim path As String = Application.StartupPath & "\Resources\Emotes\Emotes.xml"
        If File.Exists(path) Then
            File.Delete(path)
        End If
        Dim serializer As New XmlSerializer(GetType(List(Of emote)))
        Dim stream As New FileStream(path, FileMode.OpenOrCreate)
        serializer.Serialize(DirectCast(stream, Stream), list)
        stream.Close()
        stream.Dispose()
    End Sub

    Public Shared Property SmiliesList() As List(Of emote)
        Get
            Return lstSmilies
        End Get
        Set(ByVal value As List(Of emote))
            lstSmilies = value
        End Set
    End Property
End Class


Here's the XML (There will be more here eventually):

<?xml version="1.0"?>
<ArrayOfEmote>
  <emote>
    <EmoteId>1</EmoteId>
    <Title>smile</Title>
    <Shortcuts>
      <string>:)/></string>
      <string>:-)</string>
      <string>(-:</string>
    </Shortcuts>
  </emote>
  <emote>
    <EmoteId>2</EmoteId>
    <Title>frown</Title>
    <Shortcuts>
      <string>:(/></string>
	  <string>:-(</string>
    </Shortcuts>
  </emote>
  <emote>
    <EmoteId>3</EmoteId>
    <Title>laugh</Title>
    <Shortcuts>
      <string>:)/>)</string>
      <string>:-))</string>
    </Shortcuts>
  </emote>
</ArrayOfEmote>


Here's where I call it all and haven't been able to get it going:

    Public Function ProcessEmotes() As String
        Dim strText As String = "Hi :)/>, How are you :)/>)"
        For Each Emoticon As emote In clsEmotes.SmiliesList
            Dim regExp As New Regex(Emoticon.SmileyRegex.ToString)
            Dim matches As MatchCollection
            matches = regExp.Matches(strText)
            For Each aMatch As Match In matches
                'This is the last thing I had tried
            Next aMatch
            Application.DoEvents()
        Next
        ProcessEmotes = strText
    End Function


I've had it a couple of different ways and each time I've only gotten it to where it would find and replace one emote only when there were multiple emotes or would replace the emote characters ": )" and then in the second emote it would return ": )" and leave the last ")" afterwards.

Basically, it will need to go through the parent string, find the characters that match any of the emotes exactly, and replace those characters with the image path(emoteID).

This is my first time really getting into XML and Regex so I apologize if it's sloppy to any of you.

Is This A Good Question/Topic? 0
  • +

Replies To: Finding/replacing multiple sets of string within a parent string

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Finding/replacing multiple sets of string within a parent string

Posted 29 March 2012 - 12:02 AM

It help if you reorder the emotes, so that their regex are in descending order of length. Eg the one that replace the most are done first.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1