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.
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.
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:
Source: https://ru.stackoverflow.com/questions/8419/
All Articles