• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

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'

0
ericwong27
Asked:
ericwong27
  • 4
  • 4
  • 2
  • +1
1 Solution
 
SanclerCommented:
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
0
 
SanclerCommented:
For real, there ought to be some error checking ;-)

Roger
0
 
Fernando SotoCommented:
Hi ericwong27;

This should do it for you.

Imports System.Text.RegularExpressions

        Dim input As String = "Back color, Red color, Yellow color"
        Dim output As String

        output = Regex.Replace(input, "(.+)\s*,\s*(.+)", "$1 and $2")

        MessageBox.Show(output)


Fernando
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Fernando SotoCommented:
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
0
 
VeryNiceManCommented:
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;
0
 
Fernando SotoCommented:
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
0
 
VeryNiceManCommented:
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;
0
 
SanclerCommented:
>>
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

0
 
SanclerCommented:
"fiar range" = "fair range"

Roger
0
 
ericwong27Author Commented:
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)



0
 
Fernando SotoCommented:
Glad I was able to help. :=)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now