JAVA - similar to preg_match_all

Discussion in 'Programming' started by 2beers, Mar 17, 2009.

  1. #1
    Hi. I've started to learn java 3 days ago. I need something similar to preg_match_all from php

    I found regex class but can you give me an example because I'm really new.

    Thank you
     
    2beers, Mar 17, 2009 IP
  2. UAA

    UAA Active Member

    Messages:
    624
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #2
    is it JAVA SE or Java Script?
     
    UAA, Mar 17, 2009 IP
  3. 2beers

    2beers Well-Known Member

    Messages:
    195
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Java SE (by Sun) :)

    Anyway I found what i was looking for but i have a small question because i see that is a little different from php and i don't know why because in the class refference it looks ok

    so...
    I want to grab the image url from a html page



    If i use the pattern
    <img src='(.*)'>

    it return the entire line some text <img src='dir file'> another text <img src='file'>

    but if i use
    it returns ok if the src doesnt contain any spaces

    so my question is why the .* is not working?

    on sun page http://java.sun.com/docs/books/tutorial/essential/regex/pre_char_classes.html it shows me that

    Thank you
     
    2beers, Mar 17, 2009 IP
  4. UAA

    UAA Active Member

    Messages:
    624
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    58
    #4
    may be you should use *.*
     
    UAA, Mar 18, 2009 IP
  5. sp00m

    sp00m Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    A bit late, but maybe that could be usefull for someone else :

    Pattern p = Pattern.compile("<img [^>]*src=[\"|']([^(\"|')]+)[\"|'][^>]*>");
    Matcher m = p.matcher(cnt);
    while(m.find()) {
        System.out.println(m.group(1));
    }
    
    Code (markup):
     
    Last edited: Jun 15, 2011
    sp00m, Jun 15, 2011 IP