?
Solved

how to use regex in java

Posted on 2011-04-23
5
Medium Priority
?
332 Views
Last Modified: 2012-05-11
Hello ,
how to extract alt the alt "Vol atterri" from this string in java with Regex?

<a href="flightdetails.asp?FLIGHT_NUMBER=0594&amp;B_DATE=201104230000&amp;B_LOCATION_1=&amp;E_LOCATION_1=" target="_self"><img src="../../template/fr/images/ata.jpg" width="40" height="10" border="1" alt="Vol atterri"></a>

Open in new window


thank you
0
Comment
Question by:keviin555
  • 2
  • 2
5 Comments
 

Author Comment

by:keviin555
ID: 35453771
and to get TEXT from this please :
<span class="style2">TEXT</span>

Open in new window


thanx alot

0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 35453784
 String s=   "<a href=\"flightdetails.asp?FLIGHT_NUMBER=0594&amp;B_DATE=201104230000&amp;B_LOCATION_1=&amp;E_LOCATION_1=\" target=\"_self\"><img src=\"../../template/fr/images/ata.jpg\" width=\"40\" height=\"10\" border=\"1\" alt=\"Vol atterri\"></a>";

Pattern p = Pattern.compile("alt=\"(.*?)\"");
Matcher m = p.matcher(s);

while(m.find()){
System.out.println(m.group(1));
}

Open in new window

0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 2000 total points
ID: 35453794

The same thing with TEXT:

      s=   "<span class=\"style2\">TEXT</span>";
 p = Pattern.compile("<span class=\"style2\">(.*?)</span>");
 m = p.matcher(s);

while(m.find()){
System.out.println(m.group(1));
}

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 35453981
Try the following - same for the other
s = s.replaceAll(".*alt=\"(.*)\".*", "$1");

Open in new window

0
 

Author Comment

by:keviin555
ID: 35454453
thank you , work perfectly :)
ps : @for_yan , check your mail please
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

755 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