-1

I have some text like this:

[image]bdaypic.jpg[caption]My pic[/caption][/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("\\[caption\\](.*?)\\[\\/caption\\]", "<figcaption>$1</figcaption>");

but i'm getting output as

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

[caption][/caption] is optional.If it there then only it should be replaced with tag.

Cœur
  • 37,241
  • 25
  • 195
  • 267
SaiCharan
  • 61
  • 4
  • 15

3 Answers3

1

The problem seems to lie within the first replace. You are capturing everything between the [image] tag and replacing it with a quoted version of the content. I merged your two expressions and got what you are after:

        String string = "[image]bdaypic.jpg[caption]My pic[/caption][/image]";
        string=string.replaceAll("\\[image\\](.*?)\\[caption\\](.*?)\\[\\/caption\\]\\[\\/image\\]","<figure><img src='$1'/><figcaption>$2</figcaption></figure>");
        System.out.println(string);

Yields:

<figure><img src='bdaypic.jpg'/><figcaption>My pic</figcaption></figure>
npinti
  • 51,780
  • 5
  • 72
  • 96
  • @kotlasaicharanreddy: Can you please define not working? – npinti Jul 02 '15 at 08:00
  • I m getting the same input nothing is getting replaced. @npinti – SaiCharan Jul 02 '15 at 08:03
  • @kotlasaicharanreddy: It is working on my machine. Are you sure you copied the code exactly from my answer? – npinti Jul 02 '15 at 08:05
  • Yes I'm sure ,I checked it again.Still the same result. @npinti – SaiCharan Jul 02 '15 at 08:10
  • @kotlasaicharanreddy: Can you please paste the code you are using here? – npinti Jul 02 '15 at 08:13
  • `string=string.replaceAll("\\[image\\](.*?)\\[caption\\](.*?)\\[\\/caption\\]\\[\\/image\\]","
    $2
    ");`
    – SaiCharan Jul 02 '15 at 08:39
  • @kotlasaicharanreddy: I've managed to replicate the issue. I think that there might be some non printable character hiding somewhere. I know it might sound daunting, but please try copying the regex by typing it out yourself instead of the usual copy-paste. – npinti Jul 02 '15 at 08:48
  • I typed it out by myself and no change in the result.@npinti – SaiCharan Jul 02 '15 at 08:53
  • @kotlasaicharanreddy: I've copied what you are using and threw it in Winmerge and it immediately showed that there is something wrong. The issue seems to be within the last `\\[\\/image\\]` block. I deleted it and wrote it again and it worked. – npinti Jul 02 '15 at 09:00
  • wrote the whole string again, input not getting replaced.@npinti – SaiCharan Jul 02 '15 at 09:07
  • Hey it worked for me now but I have another text like `[image]hello.jpg[/image]`this is not getting replaced. @npinti [caption] tag should be optional.If it present then it should get replaced,if not then i should get output without
    tags
    – SaiCharan Jul 02 '15 at 09:10
  • @kotlasaicharanreddy: I think it would make sense to check if the string contains the optional text and act accordingly. It would be quicker to implement, because then you would need to have an optional replace, which I do not think can be done within 1 line of code. – npinti Jul 02 '15 at 09:27
  • I checked it again i m getting `
    [caption]My pic[/caption]
    ` @npinti
    – SaiCharan Jul 02 '15 at 09:34
  • INPUT: `[image]bdaypic.jpg[caption]Mypic[/caption][/image]` `[image]bdaypic2.jpg[/image]` CODE 1: `string=string.replaceAll("\\[image\\](.*?)\\[caption\\](.*?)\\[\\/caption\\]\\[\\/image\\]","
    $2
    ");` OUTPUT 1: `
    My pic
    ` `[image]bdaypic2.jpg[/image]`.
    – SaiCharan Jul 02 '15 at 09:43
  • CODE 2: `string=string.replaceAll("\\[image\\](.*?)(\\[caption\\](.*?)\\[\\/caption\\])?\\[\\/image\\]","
    $2
    ");` OUPTUT 2: `
    [caption]My pic[/caption]
    ` `
    ` @npinti
    – SaiCharan Jul 02 '15 at 09:43
  • @kotlasaicharanreddy: Please explain your comments. – npinti Jul 02 '15 at 09:48
  • If I use code 1 then I'm getting OUTPUT 1 and If I use code 2 then I'm getting OUTPUT 2. CODE 1 is not working for 2nd statement of INPUT and CODE 2 is displaying extra tags[caption][/caption] in OUTPUT 2. – SaiCharan Jul 02 '15 at 09:53
  • @kotlasaicharanreddy: This seems to be a different question than the original one. If you have optional segments in your input string, then I'd recommend you have essentially an `if` statement and act accordingly, rather than trying to stuff everything within one expression. – npinti Jul 02 '15 at 10:03
0

Lot of crazy escaping but you can use:

str = str.replaceAll("\\[(image)\\]([^\\[]+)\\[(caption)\\]([^\\[]+)\\[/\\3\\]\\[/\\1\\]", 
             "<figure><img src='$2'/><figcaption>$4</figcaption></figure>");

RegEx Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I m getting the input as output.It is not getting replaced.@anubhava I don't where I'm doing a mistake. – SaiCharan Jul 02 '15 at 08:47
  • I got my mistake but I have another text like [image]hello.jpg[/image]this is not getting replaced. @anubhava [caption] tag should be optional.If it present then it should get replaced,if not then i should get output without
    tags
    – SaiCharan Jul 02 '15 at 09:13
  • No you cannot change the nature of question by posting some odd comments. Better you edit the question and show some example input/output. – anubhava Jul 02 '15 at 10:06
0

Ive created a function that will do just that:

                   function showreplaced(){
                  var str="[image]bdaypic.jpg[caption]My pic[/caption][/image]";
                 var delimitedStr=str.replace("[image]","").replace("[/caption][/image]","").replace("[caption]","|");
                   var src=delimitedStr.substring(0,delimitedStr.indexOf("|"));
                   var caption=delimitedStr.substring(delimitedStr.indexOf("|")+1);
                    var finalStr="<figure><img src='" + src + "'/><figcaption> " + caption + "</figcaption></figure>";
                 alert(finalStr);
                   }
Vance
  • 897
  • 5
  • 9