0

in user text i have some star into that and i'm trying to convert them to html regular tags, for example, user text is :

For every loop iteration, the ** value ** of the current ** array element ** is assigned to $value and ** the array pointer ** is moved by one, until it reaches the last array element.

as you see i have some words between stars such as value, array element and etc, now i want to convert first star to <b> and second star to </b>, then finally i should have:

For every loop iteration, the <b> value </b> of the current <b> array 
element </b> is assigned to $value and <b> the array pointer 
<b> is moved by one, until it reaches the last array element.

this below code which i implemented on php work fine and i'm trying to find equivalent that on java

$re = '/\*\*(.*?)\*\*/';
$str = 'For every loop iteration, the ** value ** of the current ** array element ** is assigned to $value and ** the array pointer ** is moved by one, until it reaches the last array element.';
$subst = '<b>$1</b>';

$result = preg_replace($re, $subst, $str);
echo $result;
DolDurma
  • 15,753
  • 51
  • 198
  • 377
  • Did you see this post: [https://stackoverflow.com/questions/32178763/preg-replace-equivalent-in-java](https://stackoverflow.com/questions/32178763/preg-replace-equivalent-in-java)? – dmp Sep 21 '17 at 06:50
  • @dmp yes, thats for replace, my mean is convert first and second tag – DolDurma Sep 21 '17 at 07:39
  • Check this: https://stackoverflow.com/a/60830223/6533853 – Ninja Mar 24 '20 at 11:34

1 Answers1

0

I got one solution. But its not good. Anyway I tell you about it. Keep a flag(eg: Bool boldTagStartFound = true). Find the index of the occurrence of ** in the given string. It will be an array (please refer the link https://shekhargulati.com/2010/05/04/finding-all-the-indexes-of-a-whole-word-in-a-given-string-using-java/). Iterate the array and replace the ** with the html tag using the index. If boldTagStartFound = true, then replace the ** with bold starting html tag and reset the boldTagStartFound = false. If boldTagStartFound = false, then replace the ** with bold ending html tag and reset the boldTagStartFound = true.

dmp
  • 66
  • 9