I have data in the form:
<ol>
<li>example1</li>
<li>example2</li>
<li>example3</li>
</ol>
which needs to turn into
# example1
# example2
# example3
The pound sign has to be associated with the ol html tag. I’m using java regular expressions and this is what I have so far:
info = info.replaceAll("(?s).<ol>\n(<li>(.*?)</li>\n)*</ol>","# $2");
info is a string object containing the data. Also there may be line breaks in between the li tags.When I run it, it only prints the last item. i.e the result is
# example3
example2 and example1 are missing
Any thoughts on what I’m doing wrong?
Your regex has a couple of problems:
The solution I’d recommend: don’t tie yourself in knots. Write a loop with a Matcher.find(), pulling out the matches one by one and adding them to a string buffer. It would go something like this: