Java replace all regex matches - java

I have htmlBody field which has html of a web page assigned to it. I want to check for all occurences for relative links ending in .html and for each of them to remove their extension. I do not want htmlBody.replaceAll(".html", "") because it will remove for all links and break some external links so my approach is to find all occurences that matches regex, and for each occurence to remove their extension using replaceAll() and append to sb. I tried to follow the example from official documentation but apparently it does not change any link, what could be the problem?
StringBuilder sb = new StringBuilder();
Pattern p = Pattern.compile("^\\/(.+\\\\)*(.+).(html)$");
Matcher m = p.matcher(htmlBody);
while (m.find()) {
String updatedLink = m.group().replaceAll(".html", "");
m.appendReplacement(sb, updatedLink);
}
m.appendTail(sb);

your regex was wrong, ^ match start of string, $ match end of string.
so matcher in your code will never match.
right regex like Pattern p = Pattern.compile("['\"]\\/(.+\\\\)*(.+).(html)");
but, it can't match <a href=/a.html>

Related

How to replace a given substring with "" from a given string?

I went through a couple of examples to replace a given sub-string from a given string with "" but could not achieve the result. The String is too long to post and it contains a sub-string which is as follows:-
/image/journal/article?img_id=24810&t=1475128689597
I want to replace this sub-string with "".Here the value of img_id and t can vary, so I would have to use regular expression. I tried with the following code:-
String regex="^/image/journal/article?img_id=([0-9])*&t=([0-9])*$";
content=content.replace(regex,"");
Here content is the original given string. But this code is actually not replacing anything from the content. So please help..any help would be appreciated .thanx in advance.
Use replaceAll works in nice way with regex
content=content.replaceAll("[0-9]*","");
Code
String content="/image/journal/article?img_id=24810&t=1475128689597";
content=content.replaceAll("[0-9]*","");
System.out.println(content);
Output :
/image/journal/article?img_id=&t=
Update : simple, might be little less cozy but easy one
String content="sas/image/journal/article?img_id=24810&t=1475128689597";
content=content.replaceAll("\\/image.*","");
System.out.println(content);
Output:
sas
If there is something more after t=1475128689597/?tag=343sdds and you want to retain ?tag=343sdds then use below
String content="sas/image/journal/article?img_id=24810&t=1475128689597/?tag=343sdds";
content=content.replaceAll("(\\/image.*[0-9]+[\\/])","");
System.out.println(content);
}
Output:
sas?tag=343sdds
If you're trying to replace the substring of the URL with two quotations like so:
/image/journal/article?img_id=""&t=""
Then you need to add escaped quotes \"\" inside your content assignment, edit your regex to only look for the numbers, and change it to replaceAll:
content=content.replaceAll(regex,"\"\"");
You can use Java regex Utility to replace your String with "" or (any desired String literal), based on given pattern (regex) as following:
String content = "ALPHA_/image/journal/article?img_id=24810&t=1475128689597_BRAVO";
String regex = "\\/image\\/journal\\/article\\?img_id=\\d+&t=\\d+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
String replacement = matcher.replaceAll("PK");
System.out.println(replacement); // Will print ALPHA_PK_BRAVO
}

How to extract image url from a string?

I am trying to extract image url from inside of a string. I am using Pattern and Matcher. I am using a regular expression to match the same. Whenever I am trying to debug the code, both, matcher.matches() and matcher.find() result into false.
I am attaching the image url and regular expression as well as my code.
Pattern pattern_name;
Matcher matcher_name;
String regex = "(http(s?):/)(/[^/]+)+\" + \"\\.(?:jpg|gif|png)";
String url = "http://www.medivision360.com/pharma/pages/articleImg/thumbnail/thumb3756d839adc5da3.jpg";
pattern_name = Pattern.compile(regex);
matcher_name = pattern_name.matcher(url);
matcher_name.matches();
matcher_name.find();
You seem to have some issue with the regex, the \" + \" should come from some code you mistook for a regex. That subpattern requires a quote, one or more spaces, then a space, and another double quote to appear right before the extension. It matches something like http://www.medivision360.com/pharma/pages/articleImg/thumbnail/thumb3756d839adc5da3" ".jpg.
Also, there are two redundant capture groups at the beginning, you do not need to use them.
Use
String regex = "https?:/(?:/[^/]+)+\\.(?:jpg|gif|png)";
See this demo
Java demo:
String rx = "https?:/(?:/[^/]+)+\\.(?:jpg|gif|png)";
String url = "http://www.medivision360.com/pharma/pages/articleImg/thumbnail/thumb3756d839adc5da3.jpg";
Pattern pat = Pattern.compile(rx);
Matcher matcher = pat.matcher(url);
if (matcher.matches()) {
System.out.println(matcher.group());
}
Note that Matcher#matches() requires a full string match, while Matcher#find() will find a partial match, a match inside a larger string.
You've escaped the double quotes in the string catenation
so the regex engine sees this (http(s?):/)(/[^/]+)+" + "\.(?:jpg|gif|png)
after c++ parses the string.
You can un-escape it "(http(s?):/)(/[^/]+)+" + "\\.(?:jpg|gif|png)"
or just join them together "(http(s?):/)(/[^/]+)+\\.(?:jpg|gif|png)"
If the expression is always at the end, I would suggest:
([^/?]+)(?=/?(?:$|\?))

Error while using FileUtils

I am using FileUtils from apache commons.io to search text between two strings in a file with the following code:
Pattern p = Pattern.compile(Pattern.quote(fromDate) + "(.*?)" + Pattern.quote(toDate));
try {
Matcher m = p.matcher(fileContent);
while (m.find()) {
System.out.println(m.group(1));
But there is an error it is giving output only when both the strings lie in same line, no output if strings are in at different lines? Here i am taking the content of whole file into a Sting Varibale "fileContent".
The dot won't search over multiple lines. You need to give a second parameter for this Pattern.DOTALL like so:
Pattern p = Pattern.compile(Pattern.quote(fromDate) + "(.*?)" + Pattern.quote(toDate), Pattern.DOTALL);
Also this topics has some good explanation how it works: Match multiline text using regular expression
try ending the regex with ?s so you new regex should be:
"(.*?s)"
In most case the matcher stops evaluate expression when it encounters a line feed \n.
?s make the matcher pass the \n when it try to match the regex.

Java regex URL prefix removal

I have a set of URLs. Some of them have a string www as substring and some of them haven't. I need to remove prefixes in each URL.
I tried remove this prefixes using many variants of regexp:
newStr = str.replaceAll("http://|http://www.", "");
newStr = str.replaceAll("^http://|http://www.$", "");
newStr = str.replaceAll("http://|http://www.", "");
where str - is an inputted URL string, and newStr is the URL after replacement.
Each of these variants replaces only http:// prefix, but www. remains in result. How I can change my regexp to remove http:// string as well as http://www. string?
I know that I can use replaceAll() twice:
newStr = str.replaceAll("http://", "").replaceAll("www.", "");
But what should I do to remain one replaceAll() and edit only the regular expression?
newStr = str.replaceFirst("^(http://)?(www\\.)?", "");
please note that . in regex means anything so you need to escape it, or you will strip first 4 symbols from wwwiscool.com and you probably don't want that. And you probably want to replace only the first matching prefix.
You can use str.replace, for example :
String str = "http://www.google.com";
str.replace("http://","").replace("http:// www.","").replace("www.","");
For more information about str.replace

Regular expression for matching repeated substring

I need to get URLs from background-image value in HTML style parameter, in this stage I have this regular (URL is long regular matching valid URLS so I omit it here for simplification):
background-image\s*?\:\s*?(url\(\s*?(['"])?\s*?(URL)\s*?(\2)?\s*?\)([,]?))+
It matches only the first occurrence of URL, I think I've allowed to match all occurrences (but obviously I haven't). What am I doing wrong?
Input may looks like this:
String txt = "<div style=\"background-image: url('A'), url(B);\">fooo</div>";
and what I need to achieve with my regular:
Check whether there is a background-image value followed with * spaces, then : (colon) and again * spaces.
Extract all values in url() pattern.
Now I am able to to get all values in url() pattern but I am not able to ensure that there is a background-image value.
Your regex is fine, except for that it doesn't search for URL's it searches for the text URL. I've added a \d behind URL to demonstrate that your regex works:
Pattern p = Pattern.compile("background-image\\s*?\\:\\s*?(url\\(\\s*?(['\"])?\\s*?(URL\\d)\\s*?(\\2)?\\s*?\\)([,]?))+");
Matcher m = p.matcher("background-image: url(URL1); background-image: url(URL2)");
while( m.find() ){
System.out.println(m.group(3));
}
Output:
URL1
URL2

Categories

Resources