Found lots of examples on string.replaceall but I cannot figure out how to use regex to solve my problem. I am looking to find and replace all occurrences of the string [reset_token] within my message.
Code I have so far:
String message = "Your new token is [reset_token]";
String newbody = replaceDelimiter("^[reset_token]", "mynewtoken");
public String replaceDelimiter(String delimiter, String message) {
return message.replaceAll(delimiter, message);
}
I would like the result to be "Your new token is mynewtoken"
You don't need replaceAll here, as your pattern is not really a regex, but is static. Simple replace would work fine:
String newbody = message.replace("[reset_token]", "mynewtoken");
And also you don't need that extra method wrapping your replace call.
You need replace() instead as it replaces the string token as is:
String message = "Your new token is [reset_token]";
String newbody = message.replace("[reset_token]", "mynewtoken");
Related
I want to remove the content between <script></script>tags. I'm manually checking for the pattern and iterating using while loop. But, I'm getting StringOutOfBoundException at this line:
String script = source.substring(startIndex,endIndex-startIndex);
Below is the complete method:
public static String getHtmlWithoutScript(String source) {
String START_PATTERN = "<script>";
String END_PATTERN = " </script>";
while (source.contains(START_PATTERN)) {
int startIndex=source.lastIndexOf(START_PATTERN);
int endIndex=source.indexOf(END_PATTERN,startIndex);
String script=source.substring(startIndex,endIndex);
source.replace(script,"");
}
return source;
}
Am I doing anything wrong here? And I'm getting endIndex=-1. Can anyone help me to identify, why my code is breaking.
String text = "<script>This is dummy text to remove </script> dont remove this";
StringBuilder sb = new StringBuilder(text);
String startTag = "<script>";
String endTag = "</script>";
//removing the text between script
sb.replace(text.indexOf(startTag) + startTag.length(), text.indexOf(endTag), "");
System.out.println(sb.toString());
If you want to remove the script tags too add the following line :
sb.toString().replace(startTag, "").replace(endTag, "")
UPDATE :
If you dont want to use StringBuilder you can do this:
String text = "<script>This is dummy text to remove </script> dont remove this";
String startTag = "<script>";
String endTag = "</script>";
//removing the text between script
String textToRemove = text.substring(text.indexOf(startTag) + startTag.length(), text.indexOf(endTag));
text = text.replace(textToRemove, "");
System.out.println(text);
You can use a regex to remove the script tag content:
public String removeScriptContent(String html) {
if(html != null) {
String re = "<script>(.*)</script>";
Pattern pattern = Pattern.compile(re);
Matcher matcher = pattern.matcher(html);
if (matcher.find()) {
return html.replace(matcher.group(1), "");
}
}
return null;
}
You have to add this two imports:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
I know I'm probably late to the party. But I would like to give you a regex (really tested solution).
What you have to note here is that when it comes to regular expressions, their engines are greedy by default. So a search string such as <script>(.*)</script> will match the entire string starting from <script> up until the end of the line, or end of the file depending on the regexp options used. This is due to the fact that the search engine uses greedy matching by default.
Now in order to perform the match that you want to in an accurate manner... you could use "lazy" searching.
Search with Lazy loading
<script>(.*?)<\/script>
Now with that, you will get accurate results.
You can read more about about Regexp Lazy & Greedy in this answer.
This worked for me:
private static String removeScriptTags(String message) {
String scriptRegex = "<(/)?[ ]*script[^>]*>";
Pattern pattern2 = Pattern.compile(scriptRegex);
if(message != null) {
Matcher matcher2 = pattern2.matcher(message);
StringBuffer str = new StringBuffer(message.length());
while(matcher2.find()) {
matcher2.appendReplacement(str, Matcher.quoteReplacement(" "));
}
matcher2.appendTail(str);
message = str.toString();
}
return message;
}
Credit goes to nealvs: https://nealvs.wordpress.com/2010/06/01/removing-tags-from-a-string-in-java/
I have a string that saves user login name and I want to remove specific characters from that string,i want to remove "#gmail.com" and just have the name before the #, then save it as a new string?
How can I do this?
Here's an example, email can be any email address, not just gmail.com
public class Test {
public static void main(String[] args) {
String email = "nobody#gmail.com";
String nameOnly = email.substring(0,email.indexOf('#'));
System.out.println(nameOnly);
}
}
make sure the email format be correct then use "split" method to split the string from '#' character's position and use first portion of results.
var str = "username#amailserver.com";
var res = str.split("#");
var username = res[0];
You can use regex + replaceAll method of string for eliminate it
sample:
String s = "Rod_Algonquin#company.co.nz";
String newS = s.replaceAll("#(.*).(.*)", "");
System.out.println(newS);
will work on different sites extension.
if you want .org, .net , etc then you need to change the regex #(.*).(.*)
I have a string like
String email = "mailto://abc#gmail.com";
I want to get only the email address but without using a fixed number like
email.substring(9);
Any better approach.
The String is of the URI format so you could do
String email = "mailto://abc#gmail.com";
URI uri = URI.create(email);
String address = uri.getUserInfo() + "#" + uri.getHost();
Use a regular expression:
String email = "mailto://abc#gmail.com";
// Builds a pattern with a capturing group ()
Pattern mailtoPattern = Pattern.compile("mailto://(.*)");
// Give your string to a matcher generated by the compiled pattern
Matcher mailMatcher = mailtoPattern.matcher(email);
// If your String is correctly formatted you can attempt to capture
// the content between parenthesis
if (mailMatcher.find()) {
String mailValue = emailMatcher.group(1);
}
Using regular expressions will also help you validate the String given as input, you can even validate if the mail String is indeed a mail address (there are crazy people with all sorts of crazy expressions to validate them). I recommend you read the very thorough JavaDoc here: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html.
Not using regex
String string = "mailto://abc#gmail.com";
final String PREFIX = "mailto://";
String email = string.substring(PREFIX.length());
I have the following string
http://store.aqa.org.uk/qual/newgcse/pdf/AQA-4695-W-SP.PDF
I want it so if the user forgets to input the http:// or the .PDF, the program will automatically correct this. Therefore, I tried this code
if (!str.startsWith("http://")) { // correct forgetting to add 'http://'
str = "http://" + str;
}
System.out.println(str);
if (!str.endsWith("\\Q.PDF\\E")) {
str = str + "\\Q.pdf\\E";
}
However, even when I enter the correct string, http://store.aqa.org.uk/qual/newgcse/pdf/AQA-4695-W-SP.PDF
the output is this.
http://store.aqa.org.uk/qual/newgcse/pdf/AQA-4695-W-SP.PDF\Q.pdf\E
Why? Why is another '.PDF' being added?
Because http://store.aqa.org.uk/qual/newgcse/pdf/AQA-4695-W-SP.PDF doesn't have a \Q.PDF\E on the end. In a string literal, \\ gives you a backslash. So "\\Q.PDF\\E" is \Q.PDF\E — a backslash, followed by a Q, followed by a dot, followed by PDF, followed by another backslash, followed by E.
If you want to see if the string ends with .PDF, just use
if (!str.endsWith(".PDF"))
Of course, that's case-sensitive. If you want it to be case-insensitive, probably:
if (!str.toLowerCase().endsWith(".pdf"))
Hy. I think this is what you want:
String str = "http://store.aqa.org.uk/qual/newgcse/pdf/AQA-4695-W-SP";
//String str = "http://store.aqa.org.uk/qual/newgcse/pdf/AQA-4695-W-SP.PDF";
if (!str.startsWith("http://")) { // correct forgetting to add 'http://'
str = "http://" + str;
}
System.out.println(str);
if (!str.endsWith(".PDF")) {
str = str + ".PDF";
}
System.out.println(str);
}
- Its simply because your String http://store.aqa.org.uk/qual/newgcse/pdf/AQA-4695-W-SP.PDF doesNot ends with \Q.PDF\E
- If you are concerned with matching the .PDF, then do this...
if (s.endsWith(".PDF")){
// add it at the end....
}
- It would be more appropriate to use StringBuilder here instead of String, which is mutable.
I have a string as : "This is a URL http://www.google.com/MyDoc.pdf which should be used"
I just need to extract the URL that is starting from http and ending at pdf :
http://www.google.com/MyDoc.pdf
String sLeftDelimiter = "http://";
String[] tempURL = sValueFromAddAtt.split(sLeftDelimiter );
String sRequiredURL = sLeftDelimiter + tempURL[1];
This gives me the output as "http://www.google.com/MyDoc.pdf which should be used"
Need help on this.
This kind of problem is what regular expressions were made for:
Pattern findUrl = Pattern.compile("\\bhttp.*?\\.pdf\\b");
Matcher matcher = findUrl.matcher("This is a URL http://www.google.com/MyDoc.pdf which should be used");
while (matcher.find()) {
System.out.println(matcher.group());
}
The regular expression explained:
\b before the "http" there is a word boundary (i.e. xhttp does not match)
http the string "http" (be aware that this also matches "https" and "httpsomething")
.*? any character (.) any number of times (*), but try to use the least amount of characters (?)
\.pdf the literal string ".pdf"
\b after the ".pdf" there is a word boundary (i.e. .pdfoo does not match)
If you would like to match only http and https, try to use this instead of http in your string:
https?\: - this matches the string http, then an optional "s" (indicated by the ? after the s) and then a colon.
why don't you use startsWith("http://") and endsWith(".pdf") mthods of String class.
Both the method returns boolean value, if both returns true, then your condition succeed else your condition is failed.
Try this
String StringName="This is a URL http://www.google.com/MyDoc.pdf which should be used";
StringName=StringName.substring(StringName.indexOf("http:"),StringName.indexOf("which"));
You can use Regular Expression power for here.
First you have to find Url in original string then remove other part.
Following code shows my suggestion:
String regex = "\\b(http|ftp|file)://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*[-a-zA-Z0-9+&##/%=~_|]";
String str = "This is a URL http://www.google.com/MyDoc.pdf which should be used";
String[] splited = str.split(regex);
for(String current_part : splited)
{
str = str.replace(current_part, "");
}
System.out.println(str);
This snippet code cans retrieve any url in any string with any pattern.
You cant add customize protocol such as https to protocol part in above regular expression.
I hope my answer help you ;)
public static String getStringBetweenStrings(String aString, String aPattern1, String aPattern2) {
String ret = null;
int pos1,pos2;
pos1 = aString.indexOf(aPattern1) + aPattern1.length();
pos2 = aString.indexOf(aPattern2);
if ((pos1>0) && (pos2>0) && (pos2 > pos1)) {
return aString.substring(pos1, pos2);
}
return ret;
}
You can use String.replaceAll with a capturing group and back reference for a very concise solution:
String input = "This is a URL http://www.google.com/MyDoc.pdf which should be used";
System.out.println(input.replaceAll(".*(http.*?\\.pdf).*", "$1"));
Here's a breakdown for the regex: https://regexr.com/3qmus