Can I replace regex with Android with groups from a match?

I would like to do something similar on Android:

from:

*foo* bar*stuff* 

dest:

 <b>foo</b> bar<b>stuff</b> 

In C# I created Regex and replaced:

 string text = "*foo* bar*stuff*"; Regex bold = new Regex(@"\*(.+?)\*", RegexOptions.Singleline); html = bold.Replace(html, new MatchEvaluator(p => string.Format("<b>{0}</b>", p.Groups[1].Value))); 

I read the Android java.util.regex.Matcher documentation , and it says the replace() method replaces all the matching with a string (constant). Is there a way to replace each occurrence with a link to this event group?

+4
source share
2 answers

Must be:

 "<b>$1</b>" 

This is C #, but it must be identical in Java. Similarly, $0 represents a complete match (or $& on some variants)

+2
source

Have you looked at MatchResult ?

0
source

Source: https://habr.com/ru/post/1339961/


All Articles