-1

I am very weak in regex and i think the task i have been assigned requires regex. suppose i have following string #B#Objectives:#B#We aimed to review how personality characteristics contribute to the onset, maintenance or modulation of.#B#Method:#B#The databases Medline and PsychINFO were examined from 1967 to 2012 to.

Now what my requirement is to replace #B#anystring#B# with <div class="format">anystring</div>.

I think regex is required but i have no idea what the regex would be.

Please give any suggestion.

updated

String regex="#B#";
String textFormated = text.replaceAll(regex,"<div class='formated'>");
System.out.println(textFormated);

this is what i tried till now.It just replaces #B# with <div class='formated'> but there is slight complication in adding end of div.The end of div should be added if :#B# is found.But how these both regex can be used together.

4

2 回答 2

2

Use this:

str = str.replaceAll("#B#(.*?)#B#", "<div class=\"format\">$1</div>");

.*? is reluctant qualifier. It means, that it matches as few characters as possible (posibly zero) up to the next #B#.

于 2013-05-07T05:42:09.493 回答
1

Try this code, this will help you

String givenData ="asdfasdfa#B#anystring#B#asdfasdfasdfas";
String pattern = "(#B#)(.+?)(#B#)";
String result = givenData.replaceAll(pattern, "<div class='format'>$2</div>");
System.out.println(result);
于 2013-05-07T05:48:31.713 回答