how could I extract the following with regex?
String string = "<h1>1st header</h1>" + "<h2>second header</h2>" +
"<p>some text</p>" + "<hr />";
Pattern p = Pattern.compile("</h1>(\\S+)<hr />", Pattern.MULTILINE);
Output is empty, but why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The output is empty because the characters between
</h1>and<hr />include spaces. Your\S+will fail as soon as it encounters a space.If you replace
\\S+with, say,.+, it should catch everything in your highly specific example string. However, if you’d like to do this “right”, and be able to match arbitrary HTML that doesn’t perfectly fit your example, use an HTML parser like the HTML Agility Pack. A parser-based version will be easy, correct, and won’t endanger your sanity and/or the universe.