Link to home
Start Free TrialLog in
Avatar of ericwong27
ericwong27Flag for Singapore

asked on

replace the last Comma Separated to 'and' using Regular exprssion

Anyone know how to replace the last Comma Separated to 'and' using regular expression. Your help is very appreciated.

Example

Input : 1,2,3,4
output : 1,2,3 and 4

Input : Back color, Red color, Yellow color
output : Back color, Red color and Yellow color

Input : 'This is a tree', ' This is a rock'
output : 'This is a tree' and 'This is a rock'

Avatar of Sancler
Sancler

Try this.  Form with one Button - Button1

    Private Function LastCommaToAnd(ByVal sourceText As String) As String
        Return sourceText.Substring(0, sourceText.LastIndexOf(",")) & " and " & Trim(sourceText.Substring(sourceText.LastIndexOf(",") + 1))
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox(LastCommaToAnd("1,2,3,4"))
        MsgBox(LastCommaToAnd("Back color, Red color, Yellow color"))
        MsgBox(LastCommaToAnd("'This is a tree', ' This is a rock'"))
    End Sub

Roger
For real, there ought to be some error checking ;-)

Roger
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi ericwong27;

This will test all your examples. If the input string has no commas in it then the output will be the same as the input.

        Dim input() As String = {"Back color, Red color, Yellow color", _
            "1,2,3,4", "This is a tree"", "" This is a rock"}
        Dim output As String

        For Each str As String In input
            output = Regex.Replace(str, "(.+)\s*,\s*(.+)", "$1 and $2")
            MessageBox.Show(output)
        Next

Fernando
hi;

without the overload of RegEx.

try this:

Private Function FixString(ByVal s As String) As String
        s = s.Insert(s.LastIndexOf(","), " and ")
        s = s.Remove(s.LastIndexOf(","), 1)
        Return s
End Function

best regards;
Hi VeryNiceMan;

You will need to have error checking just in case the string does not have a comma in it. Another thing is that after you do your insert of the word " and " you will end up with an extra space between the and, and the next work. Also one in front between the word and the word and if a space is used before the comma.

    Private Function FixString(ByVal s As String) As String

        ' A little error checking if the string has no comma.
        ' otherwise it will throw an error if one is not found.
        If s.LastIndexOf(",") >= 0 Then
            s = s.Insert(s.LastIndexOf(","), " and ")
            s = s.Remove(s.LastIndexOf(","), 1)
        End If

        Return s

    End Function


Fernando
hi;

actually here is how i do it from my own library:

Private Function FixString(ByVal s As String) As String
        Dim a() As String
        Dim i As Integer
        a = s.Split(",")
        If a.Length = 1 Then
            Return s.Trim
        End If

        For i = 0 To a.Length - 1
            If i = a.Length - 1 Then
                FixString &= "and " & a(i).Trim
            Else
                FixString &= a(i).Trim & ", "
            End If
        Next
        FixString = FixString.Trim.Substring(0, FixString.Length)
End Function

a cleaner faster solution and no need for any error checking, also the spaces are in the right places. also the overhead of RegEx is avoided.

hope that helps
best regards;
>>
also the spaces are in the right places
<<

>>
Input : 1,2,3,4
output : 1,2,3 and 4
<<

I'm not sure how pernickety ericwong27 really is about spaces - see, e.g.

>>
Input : 'This is a tree', ' This is a rock'
output : 'This is a tree' and 'This is a rock'
<<

where a space between ' and the second This in the Input has disappeared from the Output - but the output from your function with an input of 1,2,3,4 would be

    1, 2, 3 and 4

Still, I reckon the questioner now has a fiar range of ideas to choose from ;-).  But Fernando's probably has the edge because the question does say "using regular expression".

Roger

"fiar range" = "fair range"

Roger
Avatar of ericwong27

ASKER

Fernando,

Thank for your advise. Previously I using "^([\w\s\,]*)\s*,\s*(\w+\s*)$", "$1 and $2" but failed when white space is use such as "01 23,02,23 03"

Thank for those support this question too. Anyway I prefer regular express instead of other string manipulate method because the code look cleaner and shorter.

Example
output = Regex.Replace(str, "(.+)\s*,\s*(.+)", "$1 and $2")   (1 line of code)



Glad I was able to help. :=)