Regex to find String with square bracket and replace
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
add a comment |
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
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
add a comment |
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
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
java regex replace
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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);
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
add a comment |
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)
add a comment |
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);
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
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
add a comment |
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);
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
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
edited Nov 15 '18 at 5:29
answered Nov 15 '18 at 5:20
KartikKartik
3,85231435
3,85231435
add a comment |
add a comment |
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);
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
add a comment |
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);
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
add a comment |
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);
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);
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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