dlcnet
asked on
Regular Expression
Hi Experts!
I need a regex that will filter out the tile of the image
Input : <img src="x.jpg" alt="image" title="mysooper dooper image" />
Output <img src="x.jpg" alt="image" title=" " />
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You really need the following for tolerance though
s = s.replaceAll("title\\s*=\\s*\".*?\"", "title=\"\"");
ASKER
@ CEHJ
Hi ! I tried both of them and the title of the image is still there :(
Hi ! I tried both of them and the title of the image is still there :(
Please show your actual input where it failed
The following is the output from the code below
<img src="x.jpg" alt="image" title="" />
<img src="x.jpg" alt="image" title="" />
String s = "<img src=\"x.jpg\" alt=\"image\" title=\"mysooper dooper image\" /> ";
s = s.replaceAll("title\\s*=\\s*\".*?\"", "title=\"\"");
System.out.println(s);
How about:
String source = "<img src=\"x.jpg\" alt=\"image\" title=\"mysooper dooper image\" />";
String result = source.replaceAll("<img [^>]*)title=\"[^\"]*\"([^>]*)", "$1$2");
Hmmm... I misread the question : (
Correction:
Correction:
String source = "<img src=\"x.jpg\" alt=\"image\" title=\"mysooper dooper image\" />";
String result = source.replaceAll("<img [^>]*title=\")[^\"]*(\"[^>]*)", "$1$2");
ASKER
@CEHJ
my bad :) it works ... however if I have something like this is crashes:
title="blablal&&
bla
bla
bla"
title spans over multiple lines. I believe after each line is a CR
my bad :) it works ... however if I have something like this is crashes:
title="blablal&&
bla
bla
bla"
title spans over multiple lines. I believe after each line is a CR
OK. Try
s = s.replaceAll("(?s)title\\s*=\\s*\".*?\"", "title=\"\"");
Although I did forget an opening parentheses, the pattern I posted should account for multiple lines. Corrected paren below:
String result = source.replaceAll("(<img [^>]*title=\")[^\"]*(\"[^>]*)", "$1$2");
@CEHJ
That won't work either unless you turn on single-line mode : )
That won't work either unless you turn on single-line mode : )
Never mind. I missed it : (
This works for me; I just tested:
Output:
String ss = "<img src=\"x.jpg\" alt=\"image\" title=\"mysooper dooper image\" /> ";
ss= ss.replaceAll("title=\"(.*?)\"","");
System.out.println(ss);
Output:
<img src="x.jpg" alt="image" />
Or this way if you want the word title= to leave there:
String ss = "<img src=\"x.jpg\" alt=\"image\" title=\"mysooper dooper image\" /> ";
ss= ss.replaceAll("title=\"(.*?)\"","title=\"\"");
System.out.println(ss);
Output:<img src="x.jpg" alt="image" title="" />
>>ss= ss.replaceAll("title=\"(.* ?)\"","tit le=\"\"");
The group is redundant and simply creates overhead. The pattern will fail for multiline
The group is redundant and simply creates overhead. The pattern will fail for multiline
String regexString ="title=\"(.*)\"";
Pattern p = Pattern.compile(regexStrin g);
String one = "<img src=\"x.jpg\" alt=\"image\" title=\"mysooper dooper image\" />";
String two = "<img src=\"x.jpg\" alt=\"image\" title=\" \" />";
Matcher matcher = p.matcher(one);
if(matcher.find())
{
System.out.println(matcher .group(1)) ;
}
Pattern p = Pattern.compile(regexStrin
String one = "<img src=\"x.jpg\" alt=\"image\" title=\"mysooper dooper image\" />";
String two = "<img src=\"x.jpg\" alt=\"image\" title=\" \" />";
Matcher matcher = p.matcher(one);
if(matcher.find())
{
System.out.println(matcher
}
sorry i believe i am repeating the answer. sorry for that.
This works with multiline title:
String ss = "<img src=\"x.jpg\" alt=\"image\" title=\"mysooper "+ System.getProperty("line.seprator") + "dooper image\" /> ";
ss= ss.replaceAll("title=\"([^\r\n]*?)\"","title=\"\"");
System.out.println("result: " + ss);
Output:result: <img src="x.jpg" alt="image" title="" />
Yes, true, group is not necessary, I first thought that filter out means oppositely to extract;
group is from that time
group is from that time
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
group is necessary if u would like to get the title name.
Open in new window
Output:
Open in new window