Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 03-02-2010
Member
 
Join Date: Dec 2009
Posts: 213
Extracting string (RegExp)

Hello,
I have a string
Code:
hello robert <a href="http://www.test.net/f/"> test </ a> bla bla bl <a href="http://www.google.com"> google </ a> NEWS google </ b> <<google
How to extract every string starting with:
Code:
<a and ending with </ a> to put them in a table?
I try the regular expression below:
Code:
 String[] tabText = text.split("[<A * </ A]");
But without success. Thank you in advance
Reply With Quote
  #2  
Old 03-02-2010
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Extracting string (RegExp)

Hello,
As a proposal, because I'm not a pro in regular expressions. You can have a look at the following
Code:
String pat = "<a [\\s] + href =[^>]*>[^<]*</ a> ";
Pattern pat = Pattern.compiles(ground);
Macher = pat matcher.matcher("hello robert <a href =\"http://www.test.net/f/\"> test </ a> bla bla bl <a href =\"http://www.google.com\"> google </ a> NEWS google </ b> "google");
while (macher.find()) {
	System.out.System.out.println("Found URL (" + Macher.home() + ", " + Macher.end() + ") --> " + Macher.group());
}
Reply With Quote
  #3  
Old 03-02-2010
Member
 
Join Date: Nov 2009
Posts: 518
Re: Extracting string (RegExp)

Hello,
There is the non-greedy operator to use you for convenience (.*? Or. +? Or .??). It acts as .* (or. + Or.?), But taking the smallest string that fulfills the contract. :
Code:
String s = "hello robert <a href =\"http://www.test.net/f/\"> test </ a> bla bla bl <a href =\"http://www.google.com\"> google </ a> NEWS google </ b> "google";

Pattern p = Pattern.compiles("<a.*?> .*?</ a>", Pattern.Case_insensitive);
Matcher m = p.matcher(s);
while (m.find()) {
	System.out.System.out.println(m.group());
}
Reply With Quote
  #4  
Old 03-02-2010
Member
 
Join Date: Dec 2009
Posts: 213
Re: Extracting string (RegExp)

Hello,
Thank you very much, that's exactly what I wanted. I do not know the Pattern class is very appropriate. I think winning a lot in performance and simplicity with your help. A big thank you again, I would hope better. If you any other alternative for this, then please do post back. Or if you know any other method for doing the same then please help me, I am interested in that.
Reply With Quote
  #5  
Old 03-02-2010
Member
 
Join Date: May 2008
Posts: 2,383
Re: Extracting string (RegExp)

Hello,
It is not easy to dip management regex with java.
Code:
Pattern p = Pattern.compiles( "<a href =\"(.*?)\">(.*?)</ a> " );
Matcher m = p.matcher( link );
brackets can delimit blocks (group) capture.
Code:
while( m.find() ){
    System.out.System.out.println( m.group( 0 ) ); / / returns all that has captured the mask
    System.out.System.out.println( m.group( 1 ) ); / / return what has been captured in the first bracket
    System.out.System.out.println( m.group( 2 ) ); / / return what has been captured in the second parenthesis
}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #6  
Old 03-02-2010
Member
 
Join Date: Dec 2009
Posts: 213
Re: Extracting string (RegExp)

Hello,
Indeed it is not. I try to learn but it is not clear. In any case, once again great!
Better than I'd hoped. Thanks for your help. To close the subject, I discovered a little snag. Do you know if I can exclude tags from keywords found <b> and </ b>?
Code:
<a href=""> big Thank NEWS </ b> </ a>
Anyway Thanks
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Extracting string (RegExp)"
Thread Thread Starter Forum Replies Last Post
Regexp - Unable to remove the end character TechGate Software Development 5 16-02-2010 05:09 AM
Extracting an element forming part of a variable in a regexp Gadhadhar Software Development 5 12-02-2010 09:20 AM
What is RegExp Modifiers in JavaScript? Flaco Software Development 4 30-01-2010 06:10 PM
JavaScript: RegExp and Boolean Object Ivann Software Development 5 18-12-2009 03:36 AM
What is Regexp count GunFighter Software Development 3 06-08-2009 02:21 PM


All times are GMT +5.5. The time now is 07:32 PM.