regex - Pass Java backreference to method parameter -


i need java port of https://gist.github.com/jbroadway/2836900, simple markdown regex parser in php.

i hoping use backreferences, can't make work. @ moment i'm not using hashmap, i've got 2 javafx textareas i'll , set text via changelistener.

{ //... htmltextarea.settext(markdowntextarea.gettext()     .replaceall("(#+)(.*)", header("$0", "$1", "$2")); }  private string header(string text, string char, string content) {     return string.format("<h%s>$2</h%s>", char.length(), char.length()); 

the backreference on $2 works, if returned, other backreferences don't. char.length() 2 since it's treated $2 , not backreference.

id think of solution can keep style , don't need handle separately.

the problem backreference values honored in replacement string. such, values passed header() method $0, $1 , $2 literals , not captured values.

since there's no version of replaceall() accepts lambda expression, think best bet use matcher object:

string text = "###heading 3"; pattern p = pattern.compile("(#+)(.*)"); matcher m = p.matcher(text); stringbuffer out = new stringbuffer();  while(m.find()) {     int level = m.group(1).length();     string title = m.group(2);      m.appendreplacement(out, string.format("<h%s>%s</h%s>", level, title, level)); }  m.appendtail(out);  system.out.println(out.tostring()); 

for given input, prints out:

<h3>heading 3</h3> 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -