Combining two regular expessions in java

I have some text like this:

[image]bdaypic.jpgMy pic[/image]

I should get output as:

<figure><img src='bdaypic.jpg'/><figcaption>My pic</figcaption></figure>

I’m using the below code:

string = string.replaceAll("[image](.*?)[/image]", "<figure><img src='$1'/></figure>");
string = string.replaceAll("(.*?)", "<figcaption>$1</figcaption>");

but i’m getting output as

<figure><img src='bdaypic/><figcaption>My pic</figcaption>'</figure>

is optional.If it there then only it should be replaced with tag.

Help me to resolve this??


Source: regex

Leave a Reply