Results 1 to 6 of 6

Thread: Extracting string (RegExp)

  1. #1
    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

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

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

  3. #3
    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());
    }

  4. #4
    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.

  5. #5
    Join Date
    May 2008
    Posts
    2,389

    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
    }

  6. #6
    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

Similar Threads

  1. Regexp - Unable to remove the end character
    By TechGate in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 05:09 AM
  2. Extracting an element forming part of a variable in a regexp
    By Gadhadhar in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 09:20 AM
  3. What is RegExp Modifiers in JavaScript?
    By Flaco in forum Software Development
    Replies: 4
    Last Post: 30-01-2010, 06:10 PM
  4. JavaScript: RegExp and Boolean Object
    By Ivann in forum Software Development
    Replies: 5
    Last Post: 18-12-2009, 03:36 AM
  5. What is Regexp count
    By GunFighter in forum Software Development
    Replies: 3
    Last Post: 06-08-2009, 02:21 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,047,881.32061 seconds with 16 queries