0

I need to replace part of strings, one is a RGBA and the other is a RGB, both from a CSS style-sheet on Textbox in Visual Basic.

The thing is a little more complicated, the alpha value varies and sometimes it has spaces, I guess regex is the better way to do it, but I don't know how, let me put some examples:

// RGBA
rgba(15,90,110,0.4);
rgba(15, 90,110, 0.2);
rgba(15, 90, 110, 0.35);
rgba(15, 90, 110,0.14);
rgba(15,90, 110,0.1) !important;
rgba(15,90, 110, 0.1);

// RGB
rgb(21, 25, 140);
rgb(21,25, 140);
rgb(21, 25,140);
rgb(21,25,140);
rgb(21, 25,140) !important;

// And so on...

What I'm using to replace it is the Replace function from Visual Basic:

' RGBA
TextBox2.Text = TextBox1.Text.Replace("rgba(15,90,110,0.35)", "rgba(40,133,183,0.35)")

' RGB
TextBox2.Text = TextBox1.Text.Replace("rgb(21,25,140)", "rgb(40,175,81)")

The problem comes when the alpha value changes or when it has spaces... For the RGB I replace each spacing option, but I have to do it with different colors, and the code gets too long (4 lines of code per color), is there a way to do it with regex too?

Note: I don't replace it including ; because sometimes it has the important flag, so that way I'll keep it as it is, by the way, I'm using Visual Studio 2012.

Can somebody help me out, please? Thank you in advanced.

1 Answer 1

1

Try the following function:

Private Function ReplaceRGBValue(ByVal i_sReplaceText As String, ByVal i_sOldRGBValue As String, ByVal i_sNewRGBValue As String) As String
    Dim sR As String = i_sOldRGBValue.Split(",")(0)
    Dim sG As String = i_sOldRGBValue.Split(",")(1)
    Dim sB As String = i_sOldRGBValue.Split(",")(2)
    Dim sPattern As String = "((?:rgba|rgb|RGBA|RGB)[ ]*\([ ]*)([ ]*" & sR & "[ ]*,[ ]*" & sG & "[ ]*,[ ]*" & sB & "[ ]*)(,[0-9 \.]+\)|\))"
    Dim sReplacement As String = "${1}" & i_sNewRGBValue & "${3}"
    Return System.Text.RegularExpressions.Regex.Replace(i_sReplaceText, sPattern, sReplacement)
End Function

The regex is:

(?:rgba|rgb|RGBA|RGB)[ ]*\([ ]* - Match either upper or lower case rgb or rgba, followed by 0 or more spaces, followed by open paren (escaped), followed by 0 or more spaces.

[ ]*" & sR & "[ ]*,[ ]*" & sG & "[ ]*,[ ]*" & sB & "[ ]* - Match numbers with 0 or more spaces separated by commas

(,[0-9 \.]+\)|\)) - Match a comma followed by a number, decimal point or space, or just a close paren (escaped).

The extra parentheses are capture groups that you can substitute back into the String with ${1} and ${3} in the sReplacement.

Use this function like this:

Dim sResult As String = ReplaceRGBValue("rgba(15,90,110,0.35)", "15,90,110", "40,133,183")

EDIT: To do the replacements in batch, try putting this function in a for loop like this:

  Private Sub BatchUpdate()
        ' Add more find/replace values to this array like {{"find,this,value","replace,with,this"}, _
        '                                                  {"also,find,this","and,replace,again"},...}
        Dim sReplacements(,) As String = {{"15,90,110", "40,133,183"}, _
                                          {"21,25,140", "40,175,81"}}
        Dim sResult As String = TextBox1.Text
        For i As Integer = 0 To UBound(sReplacements)
            sResult = ReplaceRGBValue(sResult, sReplacements(i, 0), sReplacements(i, 1))
        Next
        TextBox2.Text = sResult
    End Sub

I hope that works.

Sign up to request clarification or add additional context in comments.

11 Comments

Edited so it mimics more closely the behavior in OP.
Hi, it might work for manual work, the text I have is from a CSS style-sheet on Textbox, it has a lot more strings, and about 4000 lines of styles... is it possible to make the function to find the strings on the text automatically? Thanks for your help, I really appreciate it.
Using your example code, would TextBox2.Text = ReplaceRGBValue(TextBox1.Text, "15,90,110", "40,133,183") work?
Hi, no, it doesn't work, the text on TextBox1 is a CSS style-sheet with about 4000 lines
Just a note that this function will handle spaces in the text to replace. The code in the answer replaces all your examples.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.