i writing little android application using java. have text in words in brackets. want convert them links.
* [[my text]]  * [[my text]]  * [[my text]]    * <a href="http://my text">my text</a> * <a href="http://my text">my text</a> * <a href="http://my text">my text</a>   i know little regex please help.
little theory
regex allows create call capturing groups. allow use part of matched text.
for example regex x(a+)(b+)y try find text xaabbby , store 
- series of 
ain group 1 - series of 
bin group 2. 
we can few things part of matched text stored in these groups. can
- simply (and later print) using 
matcher#group(x) 
but can reuse in
- regex 
\x(wherexrepresents group number)([a-z])\1regex find pairs:aabb...yyzz, - replacement 
$x(xhere represents group number). 
your case
in case looks want replace text between [[ , ]] <a href="http://(thattext)">(thattext)</a>
to find text between [[ , ]] can use regex \[\[(.*?)\]\] 
- we surrounded 
.*?parenthesis reuse part between brackets by adding
?*?made*quantifier reluctant means instead of finding maximal match likefoo [[a]] bar [[b]] baz ^^^^^^^^^^^^^^^it try find minimal match like:
foo [[a]] bar [[b]] baz ^^^^^ ^^^^^
so can reuse part group 1 in replacement via $1. in other words can replace entire [[...]] part "<a href="http://$1">$1</a>" 
which leads solution
yourtext = yourtext.replaceall("\\[\\[(.*?)\\]\\]", "<a href=\"http://$1\">$1</a>");      
Comments
Post a Comment