Regex to find String with square bracket and replace












0















my current Code is



`
String text= "[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .";
String tag = "[School_Teacher_Name]";
String value= "Yash Mathur";
String str1 = tag.substring(1, tag.length()-1);
String reg = "/\["+str1+"\]/";

if(text.contains(tag)){
return text.replaceAll(reg, value).trim();
}
else{
return text;
}`


I dont have much experience in regex. my code is not replacing any value, Please help me out.










share|improve this question

























  • replace with what? the same string?

    – GauravRai1512
    Nov 15 '18 at 5:18













  • This is NOT JavaScript. This is Java. The two are completely different programming languages.

    – VLAZ
    Nov 15 '18 at 5:24
















0















my current Code is



`
String text= "[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .";
String tag = "[School_Teacher_Name]";
String value= "Yash Mathur";
String str1 = tag.substring(1, tag.length()-1);
String reg = "/\["+str1+"\]/";

if(text.contains(tag)){
return text.replaceAll(reg, value).trim();
}
else{
return text;
}`


I dont have much experience in regex. my code is not replacing any value, Please help me out.










share|improve this question

























  • replace with what? the same string?

    – GauravRai1512
    Nov 15 '18 at 5:18













  • This is NOT JavaScript. This is Java. The two are completely different programming languages.

    – VLAZ
    Nov 15 '18 at 5:24














0












0








0








my current Code is



`
String text= "[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .";
String tag = "[School_Teacher_Name]";
String value= "Yash Mathur";
String str1 = tag.substring(1, tag.length()-1);
String reg = "/\["+str1+"\]/";

if(text.contains(tag)){
return text.replaceAll(reg, value).trim();
}
else{
return text;
}`


I dont have much experience in regex. my code is not replacing any value, Please help me out.










share|improve this question
















my current Code is



`
String text= "[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .";
String tag = "[School_Teacher_Name]";
String value= "Yash Mathur";
String str1 = tag.substring(1, tag.length()-1);
String reg = "/\["+str1+"\]/";

if(text.contains(tag)){
return text.replaceAll(reg, value).trim();
}
else{
return text;
}`


I dont have much experience in regex. my code is not replacing any value, Please help me out.







java regex replace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 5:26









Abana Clara

1,646919




1,646919










asked Nov 15 '18 at 5:16









SurendraKumar JaiswalSurendraKumar Jaiswal

10414




10414













  • replace with what? the same string?

    – GauravRai1512
    Nov 15 '18 at 5:18













  • This is NOT JavaScript. This is Java. The two are completely different programming languages.

    – VLAZ
    Nov 15 '18 at 5:24



















  • replace with what? the same string?

    – GauravRai1512
    Nov 15 '18 at 5:18













  • This is NOT JavaScript. This is Java. The two are completely different programming languages.

    – VLAZ
    Nov 15 '18 at 5:24

















replace with what? the same string?

– GauravRai1512
Nov 15 '18 at 5:18







replace with what? the same string?

– GauravRai1512
Nov 15 '18 at 5:18















This is NOT JavaScript. This is Java. The two are completely different programming languages.

– VLAZ
Nov 15 '18 at 5:24





This is NOT JavaScript. This is Java. The two are completely different programming languages.

– VLAZ
Nov 15 '18 at 5:24












3 Answers
3






active

oldest

votes


















1














If I don't misunderstood you requirements then you can do this way. Regex



Java



import java.util.regex.Matcher;
import java.util.regex.Pattern;

final String regex = "\[(School_Teacher_Name)]";
final String string = "[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .";
final String subst = "Yash Mathur";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);

System.out.println("Substitution result: " + result);


Javascript






const regex = /[(School_Teacher_Name)]/gm;
const str = `[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .`;
const subst = `Yash Mathur`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);








share|improve this answer
























  • Thanks allot, It worked, I do not require pattern and matcher, my String class method replaceAll takes regex.

    – SurendraKumar Jaiswal
    Nov 15 '18 at 5:35











  • @SurendraKumarJaiswal You're welcome, Glad it helps you somehow

    – Always Sunny
    Nov 15 '18 at 5:46











  • May I ask why at end "]", we didnot use "//]" ?

    – SurendraKumar Jaiswal
    Nov 15 '18 at 5:51



















1














Remove forward slashes from your regex.



Change



String reg = "/\["+str1+"\]/";


to



String reg = "\["+str1+"\]";


Output



Yash Mathur is our new member, So please congratulate Yash Mathur .


PS - In this case it's better to use text.replace(tag, value) instead of text.replaceAll(reg, value)






share|improve this answer

































    0














    It's far easier with Template Literals.






    let teacher = 'Yash Mathur';
    let test = `${teacher} is our new member, so please congratulate ${teacher}.`;

    console.log(test);








    share|improve this answer
























    • OP used the wrong tag. It's not javascript

      – Abana Clara
      Nov 15 '18 at 5:23











    • Well, then I guess use replace :)

      – Will
      Nov 15 '18 at 5:27













    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312863%2fregex-to-find-string-with-square-bracket-and-replace%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    If I don't misunderstood you requirements then you can do this way. Regex



    Java



    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    final String regex = "\[(School_Teacher_Name)]";
    final String string = "[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .";
    final String subst = "Yash Mathur";

    final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
    final Matcher matcher = pattern.matcher(string);

    // The substituted value will be contained in the result variable
    final String result = matcher.replaceAll(subst);

    System.out.println("Substitution result: " + result);


    Javascript






    const regex = /[(School_Teacher_Name)]/gm;
    const str = `[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .`;
    const subst = `Yash Mathur`;

    // The substituted value will be contained in the result variable
    const result = str.replace(regex, subst);

    console.log('Substitution result: ', result);








    share|improve this answer
























    • Thanks allot, It worked, I do not require pattern and matcher, my String class method replaceAll takes regex.

      – SurendraKumar Jaiswal
      Nov 15 '18 at 5:35











    • @SurendraKumarJaiswal You're welcome, Glad it helps you somehow

      – Always Sunny
      Nov 15 '18 at 5:46











    • May I ask why at end "]", we didnot use "//]" ?

      – SurendraKumar Jaiswal
      Nov 15 '18 at 5:51
















    1














    If I don't misunderstood you requirements then you can do this way. Regex



    Java



    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    final String regex = "\[(School_Teacher_Name)]";
    final String string = "[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .";
    final String subst = "Yash Mathur";

    final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
    final Matcher matcher = pattern.matcher(string);

    // The substituted value will be contained in the result variable
    final String result = matcher.replaceAll(subst);

    System.out.println("Substitution result: " + result);


    Javascript






    const regex = /[(School_Teacher_Name)]/gm;
    const str = `[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .`;
    const subst = `Yash Mathur`;

    // The substituted value will be contained in the result variable
    const result = str.replace(regex, subst);

    console.log('Substitution result: ', result);








    share|improve this answer
























    • Thanks allot, It worked, I do not require pattern and matcher, my String class method replaceAll takes regex.

      – SurendraKumar Jaiswal
      Nov 15 '18 at 5:35











    • @SurendraKumarJaiswal You're welcome, Glad it helps you somehow

      – Always Sunny
      Nov 15 '18 at 5:46











    • May I ask why at end "]", we didnot use "//]" ?

      – SurendraKumar Jaiswal
      Nov 15 '18 at 5:51














    1












    1








    1







    If I don't misunderstood you requirements then you can do this way. Regex



    Java



    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    final String regex = "\[(School_Teacher_Name)]";
    final String string = "[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .";
    final String subst = "Yash Mathur";

    final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
    final Matcher matcher = pattern.matcher(string);

    // The substituted value will be contained in the result variable
    final String result = matcher.replaceAll(subst);

    System.out.println("Substitution result: " + result);


    Javascript






    const regex = /[(School_Teacher_Name)]/gm;
    const str = `[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .`;
    const subst = `Yash Mathur`;

    // The substituted value will be contained in the result variable
    const result = str.replace(regex, subst);

    console.log('Substitution result: ', result);








    share|improve this answer













    If I don't misunderstood you requirements then you can do this way. Regex



    Java



    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    final String regex = "\[(School_Teacher_Name)]";
    final String string = "[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .";
    final String subst = "Yash Mathur";

    final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
    final Matcher matcher = pattern.matcher(string);

    // The substituted value will be contained in the result variable
    final String result = matcher.replaceAll(subst);

    System.out.println("Substitution result: " + result);


    Javascript






    const regex = /[(School_Teacher_Name)]/gm;
    const str = `[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .`;
    const subst = `Yash Mathur`;

    // The substituted value will be contained in the result variable
    const result = str.replace(regex, subst);

    console.log('Substitution result: ', result);








    const regex = /[(School_Teacher_Name)]/gm;
    const str = `[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .`;
    const subst = `Yash Mathur`;

    // The substituted value will be contained in the result variable
    const result = str.replace(regex, subst);

    console.log('Substitution result: ', result);





    const regex = /[(School_Teacher_Name)]/gm;
    const str = `[School_Teacher_Name] is our new member, So please congratulate [School_Teacher_Name] .`;
    const subst = `Yash Mathur`;

    // The substituted value will be contained in the result variable
    const result = str.replace(regex, subst);

    console.log('Substitution result: ', result);






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 15 '18 at 5:22









    Always SunnyAlways Sunny

    16.3k32847




    16.3k32847













    • Thanks allot, It worked, I do not require pattern and matcher, my String class method replaceAll takes regex.

      – SurendraKumar Jaiswal
      Nov 15 '18 at 5:35











    • @SurendraKumarJaiswal You're welcome, Glad it helps you somehow

      – Always Sunny
      Nov 15 '18 at 5:46











    • May I ask why at end "]", we didnot use "//]" ?

      – SurendraKumar Jaiswal
      Nov 15 '18 at 5:51



















    • Thanks allot, It worked, I do not require pattern and matcher, my String class method replaceAll takes regex.

      – SurendraKumar Jaiswal
      Nov 15 '18 at 5:35











    • @SurendraKumarJaiswal You're welcome, Glad it helps you somehow

      – Always Sunny
      Nov 15 '18 at 5:46











    • May I ask why at end "]", we didnot use "//]" ?

      – SurendraKumar Jaiswal
      Nov 15 '18 at 5:51

















    Thanks allot, It worked, I do not require pattern and matcher, my String class method replaceAll takes regex.

    – SurendraKumar Jaiswal
    Nov 15 '18 at 5:35





    Thanks allot, It worked, I do not require pattern and matcher, my String class method replaceAll takes regex.

    – SurendraKumar Jaiswal
    Nov 15 '18 at 5:35













    @SurendraKumarJaiswal You're welcome, Glad it helps you somehow

    – Always Sunny
    Nov 15 '18 at 5:46





    @SurendraKumarJaiswal You're welcome, Glad it helps you somehow

    – Always Sunny
    Nov 15 '18 at 5:46













    May I ask why at end "]", we didnot use "//]" ?

    – SurendraKumar Jaiswal
    Nov 15 '18 at 5:51





    May I ask why at end "]", we didnot use "//]" ?

    – SurendraKumar Jaiswal
    Nov 15 '18 at 5:51













    1














    Remove forward slashes from your regex.



    Change



    String reg = "/\["+str1+"\]/";


    to



    String reg = "\["+str1+"\]";


    Output



    Yash Mathur is our new member, So please congratulate Yash Mathur .


    PS - In this case it's better to use text.replace(tag, value) instead of text.replaceAll(reg, value)






    share|improve this answer






























      1














      Remove forward slashes from your regex.



      Change



      String reg = "/\["+str1+"\]/";


      to



      String reg = "\["+str1+"\]";


      Output



      Yash Mathur is our new member, So please congratulate Yash Mathur .


      PS - In this case it's better to use text.replace(tag, value) instead of text.replaceAll(reg, value)






      share|improve this answer




























        1












        1








        1







        Remove forward slashes from your regex.



        Change



        String reg = "/\["+str1+"\]/";


        to



        String reg = "\["+str1+"\]";


        Output



        Yash Mathur is our new member, So please congratulate Yash Mathur .


        PS - In this case it's better to use text.replace(tag, value) instead of text.replaceAll(reg, value)






        share|improve this answer















        Remove forward slashes from your regex.



        Change



        String reg = "/\["+str1+"\]/";


        to



        String reg = "\["+str1+"\]";


        Output



        Yash Mathur is our new member, So please congratulate Yash Mathur .


        PS - In this case it's better to use text.replace(tag, value) instead of text.replaceAll(reg, value)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 5:29

























        answered Nov 15 '18 at 5:20









        KartikKartik

        3,85231435




        3,85231435























            0














            It's far easier with Template Literals.






            let teacher = 'Yash Mathur';
            let test = `${teacher} is our new member, so please congratulate ${teacher}.`;

            console.log(test);








            share|improve this answer
























            • OP used the wrong tag. It's not javascript

              – Abana Clara
              Nov 15 '18 at 5:23











            • Well, then I guess use replace :)

              – Will
              Nov 15 '18 at 5:27


















            0














            It's far easier with Template Literals.






            let teacher = 'Yash Mathur';
            let test = `${teacher} is our new member, so please congratulate ${teacher}.`;

            console.log(test);








            share|improve this answer
























            • OP used the wrong tag. It's not javascript

              – Abana Clara
              Nov 15 '18 at 5:23











            • Well, then I guess use replace :)

              – Will
              Nov 15 '18 at 5:27
















            0












            0








            0







            It's far easier with Template Literals.






            let teacher = 'Yash Mathur';
            let test = `${teacher} is our new member, so please congratulate ${teacher}.`;

            console.log(test);








            share|improve this answer













            It's far easier with Template Literals.






            let teacher = 'Yash Mathur';
            let test = `${teacher} is our new member, so please congratulate ${teacher}.`;

            console.log(test);








            let teacher = 'Yash Mathur';
            let test = `${teacher} is our new member, so please congratulate ${teacher}.`;

            console.log(test);





            let teacher = 'Yash Mathur';
            let test = `${teacher} is our new member, so please congratulate ${teacher}.`;

            console.log(test);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 5:22









            WillWill

            1,80411211




            1,80411211













            • OP used the wrong tag. It's not javascript

              – Abana Clara
              Nov 15 '18 at 5:23











            • Well, then I guess use replace :)

              – Will
              Nov 15 '18 at 5:27





















            • OP used the wrong tag. It's not javascript

              – Abana Clara
              Nov 15 '18 at 5:23











            • Well, then I guess use replace :)

              – Will
              Nov 15 '18 at 5:27



















            OP used the wrong tag. It's not javascript

            – Abana Clara
            Nov 15 '18 at 5:23





            OP used the wrong tag. It's not javascript

            – Abana Clara
            Nov 15 '18 at 5:23













            Well, then I guess use replace :)

            – Will
            Nov 15 '18 at 5:27







            Well, then I guess use replace :)

            – Will
            Nov 15 '18 at 5:27




















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312863%2fregex-to-find-string-with-square-bracket-and-replace%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python