Android Question Regex Matcher - Problem with multiple matches

RichardN

Well-Known Member
Licensed User
Longtime User
I am using the subroutine below to insert (bold) tags {b} into a multi-line string that I later format with a rich string formatter. The pattern is based upon selecting everything on a new line as far as a colon ':' character.

At the moment it works perfectly until it encounters a string that contains more than one instance of the pattern on different lines... then it ignores them both and skips the tags. What have I misunderstood here?

B4X:
Sub InsertBoldTags(Subject As String) As String

    Dim Pattern As String   
    Dim Matcher1 As Matcher
   
    Pattern = "^.+:"
    Matcher1 = Regex.Matcher2(Pattern, Regex.MULTILINE , Subject)
               
    Do While Matcher1.Find
       
          Subject = Subject.Replace(Matcher1.Match,"{b}" & Matcher1.Match & "{b}")
   
    Loop
   
    Return Subject

End Sub
 

RichardN

Well-Known Member
Licensed User
Longtime User
Hi Erel thanks for the reply. I have modified your code slightly to pass var 'Tags' but the method is still not suitable for my purpose as I need to return every instance of the matched characters exactly as they are only bracketed by my {b} tags. Universal replacement using your code is easy but I need selective replacement...

I need to get from (text file):...

Jan: It rained
Feb: Sunshine
Nothing here
Mar 31: My Birthday
Nothing here either
May-Jun: It was windy

To.....

{b}Jan:{b} It rained
{b}Feb:{b} Sunshine
Nothing here
{b}Mar 31:{b} My Birthday
Nothing here either
{b}May-Jun:{b} It was windy

I am struggling to get to the 'found' part of the regex string to concantate it as part of the return variable. I have guessed at this code but it throws a 'java.lang.illegalStateException: No successful match found so far' when it hits r.runmethod2....

B4X:
Sub AddTags

    Dim Event As String      
    Event = EventCursor.GetString("Event")
      
    Event = RegexReplace("^.+:", Event, "{b}")
              
    rsb.Append(Event & CRLF & CRLF)

End Sub


Sub RegexReplace (Pattern As String, Text As String, Tag As String) As String

  Dim m As Matcher
  m = Regex.Matcher2(Pattern,Regex.MULTILINE , Text)

  Dim r As Reflector
  r.Target = m

  Return  r.RunMethod2("replaceAll", Tag & m.Match & Tag, "java.lang.String")

End Sub


I am guessing the problem is with the m.Match in the function but I can't see how to resolve it.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim s As String = "Jan: It rained" & CRLF & "Feb: Sunshine"
   Dim s2 As String = RegexReplace("^([^:]+)\:", s, "\{B}$1\{B}")
   Log(s2)
End Sub

Sub RegexReplace(Pattern As String, Text As String, Replacement As String) As String
  Dim m As Matcher
  m = Regex.Matcher2(Pattern, Regex.MULTILINE, Text)
  Dim jo As JavaObject = m
  Return jo.RunMethod("replaceAll", Array(Replacement))
End Sub

See this tutorial: http://www.regular-expressions.info/replacebackref.html
 
Upvote 0

RichardN

Well-Known Member
Licensed User
Longtime User
Thanks Erel.

The requirement to pick out multiple regex string instances is a fairly basic programming need.... and to support 'replace-back references' almost as important. As this functionality is already supported by regex in Java do you have any plans to expose it in the B4A Regex/Matcher functionality ?
 
Upvote 0
Top