Android Question Combine two patterns in the same edit text: csbuilder

Pathrose

Member
Licensed User
Hi.
Referring to this example https://www.b4x.com/android/forum/t...ing-based-on-regex-pattern.83002/#post-525565
I would like to color two words in the same edit text with two different patterns. But when I apply the above example only one pattern works..how do I combine two patterns in the same edit text field
Regards...Here is my code
B4X:
Dim s As String = edtDose.Text

    Dim cs As CSBuilder = MarkPattern(s, "\B(℗\w+)\b", 1)

    edtDose.Text = cs

Dim y As String = edtDose.Text

   Dim cs As CSBuilder = MarkRoute(y, "\B:)\w+)\b", 1)

    edtDose.Text = cs
 
Last edited:

drgottjr

Expert
Licensed User
Longtime User
you are overwiting the first string with the second. with csbuilder, you add each successive string to the builder. when you're done building, you call builder.toString.
B4X:
dim cs as csbuilder
cs.initialize
cs.append(MarkPattern(s, "\B(℗\w+)\b", 1) ).append( MarkRoute(y, "\B:)\w+)\b", 1))
edtdose.text = cs.tostring
 
Upvote 0

Pathrose

Member
Licensed User
Thank you for your reply..
It is repeating the the edtDose.text without any effect..below is my complete code
B4X:
Dim s As String = edtDose.Text

Dim cs As CSBuilder = MarkPattern(s, "\B(℗\w+)\b", 1)

edtDose.Text = cs

Dim y As String = edtDose.Text

Dim cs As CSBuilder = MarkRoute(y, "\B:)\w+)\b", 1)

edtDose.Text = cs


Sub MarkPattern(Input As String, Pattern As String, GroupNumber As Int) As CSBuilder

    Dim cs As CSBuilder

    cs.Initialize

    Dim lastMatchEnd As Int = 0

    Dim m As Matcher = Regex.Matcher(Pattern, Input)

  

    Do While m.Find

        Dim currentStart As Int = m.GetStart(GroupNumber)

        Log(GroupNumber)

        Log(currentStart)

        cs.Append(Input.SubString2(lastMatchEnd, currentStart))

      

        Dim AA As String = Input.SubString2(lastMatchEnd, currentStart)

        Log(AA)

        lastMatchEnd = m.GetEnd(GroupNumber)

        Log(lastMatchEnd)

          

      

        'cs.Bold.Underline

        cs.Underline

        'cs.Color(0xFFEE82EE)

        cs.Color(0xFF4169E1)

        cs.Clickable("cs", m.Group(GroupNumber))

        cs.Append(m.Group(GroupNumber))

        'cs.Pop.Pop.Pop.Pop 'number should match number of stylings set.

        cs.Pop.Pop.Pop

    Loop

    If lastMatchEnd < Input.Length Then cs.Append(Input.SubString(lastMatchEnd))

    Return cs

End Sub



Sub MarkRoute(Input As String, Pattern As String, GroupNumber As Int) As CSBuilder

    Dim cs As CSBuilder

    cs.Initialize

    Dim lastMatchEnd As Int = 0

    Dim m As Matcher = Regex.Matcher(Pattern, Input)

  

    Do While m.Find

        Dim currentStart As Int = m.GetStart(GroupNumber)

        Log(GroupNumber)

        Log(currentStart)

        cs.Append(Input.SubString2(lastMatchEnd, currentStart))

      

        Dim AA As String = Input.SubString2(lastMatchEnd, currentStart)

        Log(AA)

        lastMatchEnd = m.GetEnd(GroupNumber)

        Log(lastMatchEnd)

          

        'apply styling here

        cs.Underline

        'cs.Color(0xFF6B8E23)

        cs.Color(0xFFD1650B)

        cs.Clickable("cs", m.Group(GroupNumber))

        cs.Append(m.Group(GroupNumber))

        cs.Pop.Pop.Pop 'number should match number of stylings set.

    Loop

    If lastMatchEnd < Input.Length Then cs.Append(Input.SubString(lastMatchEnd))

    Return cs

End Sub
please help
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
you keep doing the same thing:
B4X:
Dim s As String = edtDose.Text
Dim cs As CSBuilder = MarkPattern(s, "\B(℗\w+)\b", 1)   ' <<<<< THIS ONE IS OVERWRITTEN BY THE NEXT ONE
edtDose.Text = cs
Dim y As String = edtDose.Text
Dim cs As CSBuilder = MarkRoute(y, "\B:)\w+)\b", 1)     ' <<<<<< THIS ONE OVERWRITES THE PREVIOUS ONE
edtDose.Text = cs                                                            ' <<<<<< FIRST CS IS GONE BECAUSE YOU OVERWROTE IT

you are declaring 2 instances of csbuilder: #2 overwrites #1
have markpattern() and markroute() return a string. then do what i showed previously:
B4X:
dim cs as csbuilder
cs.initialize
cs.append(MarkPattern(s, "\B(℗\w+)\b", 1) ).append( MarkRoute(y, "\B:)\w+)\b", 1))
edtdose.text = cs.tostring


and please use the code tags when posting code
 
Upvote 0

Pathrose

Member
Licensed User
you keep doing the same thing:
B4X:
Dim s As String = edtDose.Text
Dim cs As CSBuilder = MarkPattern(s, "\B(℗\w+)\b", 1)   ' <<<<< THIS ONE IS OVERWRITTEN BY THE NEXT ONE
edtDose.Text = cs
Dim y As String = edtDose.Text
Dim cs As CSBuilder = MarkRoute(y, "\B:)\w+)\b", 1)     ' <<<<<< THIS ONE OVERWRITES THE PREVIOUS ONE
edtDose.Text = cs                                                            ' <<<<<< FIRST CS IS GONE BECAUSE YOU OVERWROTE IT

you are declaring 2 instances of csbuilder: #2 overwrites #1
have markpattern() and markroute() return a string. then do what i showed previously:
B4X:
dim cs as csbuilder
cs.initialize
cs.append(MarkPattern(s, "\B(℗\w+)\b", 1) ).append( MarkRoute(y, "\B:)\w+)\b", 1))
edtdose.text = cs.tostring


and please use the code tags when posting code
 
Upvote 0

Pathrose

Member
Licensed User
Thanks for your reply..
I don't think it works. It only appends the string, no color. Below is my code as suggested by you..please correct
B4X:
Dim s As String = edtDose.Text
    Dim cs As CSBuilder
    cs.initialize
    cs.append(MarkPattern(s, "\B(℗\w+)\b", 1) ).append( MarkRoute(s, "\B(:\w+)\b", 1))
    edtDose.text = cs.ToString
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
i don't lose the colors. try this way:

B4X:
Dim label1 As Label
label1.Initialize("lbl")
Activity.AddView(label1,0%x,0%y,100%x,20%y)
Dim cs1 As CSBuilder
Dim cs2 As CSBuilder

cs1 = do1
cs2 = do2
' TWO WAYS:
'label1.Text = cs1.Append(cs2)
' OR
'label1.Text = cs1.Append(do2)
' either way works
' even this works:
' label1.Text = do1.Append(do2)

       

Sub do1 As CSBuilder
    Dim cstmp As CSBuilder
    cstmp.Initialize
    cstmp.Color(Colors.Green).Append("hello ").Pop
    Return cstmp  
End Sub

Sub do2 As CSBuilder
    Dim cstmp2 As CSBuilder
    cstmp2.Initialize
    cstmp2.Color(Colors.red).Append("good-bye").Pop
    Return cstmp2
   
End Sub

leave your subs the way they way (return cs).
note: it was definitely a mistake to overwrite 1 csbuilder with another. i hope that is clear. you append the 2nd cs to the 1st. you can either append the 2 csbuilder objects or the 2 subs (since they return a csbuilder).

i cannot comment on the code in your 2 subs. if you are still losing the color change, there may be a problem in the subs. i really have no way of testing that.
 
Last edited:
Upvote 0
Top