Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Regular Expression

Posted on 2011-05-11
22
Medium Priority
?
241 Views
Last Modified: 2012-05-11

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=" " />
0
Comment
Question by:dlcnet
  • 6
  • 6
  • 5
  • +2
22 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 800 total points
ID: 35736002
Try
s = s.replaceAll("title=\".*?\"", "title=\"\"");

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35736041
                     String ss =   "<img src=\"x.jpg\" alt=\"image\" title=\"mysooper dooper image\" /> ";


Pattern p = Pattern.compile("title=\"(.*?)\"");
        
        Matcher m = p.matcher(ss);
        while (m.find()) {
           System.out.println(m.group(1));
        }

Open in new window


Output:
mysooper dooper image

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35736080
You really need the following for tolerance though
s = s.replaceAll("title\\s*=\\s*\".*?\"", "title=\"\"");

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dlcnet
ID: 35736817
@ CEHJ

 Hi ! I tried both of them and the title of the image is still there :(
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35737092
Please show your actual input where it failed
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35737754
The following is the output from the code below

<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);

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35737805
How about:
String source = "<img src=\"x.jpg\" alt=\"image\" title=\"mysooper dooper image\" />";

String result = source.replaceAll("<img [^>]*)title=\"[^\"]*\"([^>]*)", "$1$2");

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35737822
Hmmm...  I misread the question  : (

Correction:
String source = "<img src=\"x.jpg\" alt=\"image\" title=\"mysooper dooper image\" />";

String result = source.replaceAll("<img [^>]*title=\")[^\"]*(\"[^>]*)", "$1$2");

Open in new window

0
 

Author Comment

by:dlcnet
ID: 35737951
@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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35737999
OK. Try
s = s.replaceAll("(?s)title\\s*=\\s*\".*?\"", "title=\"\"");

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35738002
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");

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35738007
@CEHJ

That won't work either unless you turn on single-line mode  : )
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35738009
Never mind. I missed it  : (
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35738150
This works for me; I just tested:

    String ss =   "<img src=\"x.jpg\" alt=\"image\" title=\"mysooper dooper image\" /> ";
       ss= ss.replaceAll("title=\"(.*?)\"","");
        System.out.println(ss);

Open in new window


Output:
<img src="x.jpg" alt="image"  /> 
 

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35738168
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);

Open in new window

Output:
<img src="x.jpg" alt="image" title="" /> 

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35738481
>>ss= ss.replaceAll("title=\"(.*?)\"","title=\"\"");

The group is redundant and simply creates overhead. The pattern will fail for multiline
0
 
LVL 4

Expert Comment

by:msk_apk
ID: 35738783
     String regexString ="title=\"(.*)\"";
            Pattern p = Pattern.compile(regexString);
            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));
            }
0
 
LVL 4

Expert Comment

by:msk_apk
ID: 35738795
sorry i believe i am repeating the answer. sorry for that.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35739281
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);

Open in new window

Output:

result: <img src="x.jpg" alt="image" title="" /> 

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 35739309
Yes, true, group is not necessary, I first thought that filter out means oppositely to extract;
group is from that time
0
 
LVL 47

Accepted Solution

by:
for_yan earned 1200 total points
ID: 35739339


The same thing works without group:
                 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 without group: " +  ss);

Open in new window


result without group: <img src="x.jpg" alt="image" title="" /> 

Open in new window

0
 
LVL 4

Expert Comment

by:msk_apk
ID: 35739440
group is necessary if u would like to get the title name.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question