String regex = "href=\"(.*?)\""; 

The result will be the string:

 href="http://someurl.ru" 

Is it possible to write a regex that will not include href = "at the beginning and" at the end of the line? Thank.

    1 answer 1

    So you remove the brackets, or take the zero result from the matcher (whole line)

    UPD

     import java.util.regex.*; public class Main { public static void main(String[] args) throws Exception { String mySource = "<a href=\"http://test1.ru\" /> <a href=\"http://test2.ru\" />"; Matcher m = Pattern.compile("href=\"(.*?)\"").matcher(mySource); while(m.find()) { MatchResult mr = m.toMatchResult(); System.out.println(mr.group(0)); // entire match string System.out.println(mr.group(1)); // first group } } } 

    Result:

     cy6ergn0m@cgmachine:~/tests/java/27> javac Main.java cy6ergn0m@cgmachine:~/tests/java/27> java Main href="http://test1.ru" http://test1.ru href="http://test2.ru" http://test2.ru cy6ergn0m@cgmachine:~/tests/java/27> 

    UPD2

    References:

    • Example write pzhl. - Jenkamen
    • mr.group (1) is that in brackets "(. *?)"? - Jenkamen
    • one
      You can put any number of brackets in the expression. Then MatchResult will contain all the results from all the brackets, including the case when the brackets are nested. In this case, you have one pair of brackets, and that’s one group. So yes, mr.group (1) is in brackets. - cy6erGn0m
    • one
      I advise you to read the links from UPD2. It will not take much time, but it may clarify the situation for you. - cy6erGn0m
    • Cool! All fucking works. - Jenkamen