We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importjava.io.*;importjava.util.*;importjava.util.regex.Pattern;importjava.util.regex.Matcher;publicclassSolution{publicstaticvoidmain(String[]args){//read all input to 'markup' without newline characterScannerscanner=newScanner(System.in);Stringmarkup="";while(scanner.hasNextLine())markup=markup+scanner.nextLine();// Pattern pattern = Pattern.compile("href=\"/questions/(\\d+)/.*?class=\"question-hyperlink\">(.*?)<.*?class=\"relativetime\">(.*?)</span>"); //works GOODPatternpattern=Pattern.compile("question-summary-(\\d+).*?class=\"question-hyperlink\">(.*?)<.*?class=\"relativetime\">(.*?)</span>");Matchermatcher=pattern.matcher(markup);while(matcher.find())System.out.println(String.join(";",matcher.group(1),matcher.group(2),matcher.group(3)));}}/*(.*?) matches any character (.) any number of times (*), as few times as possible to make the regex match (?). You'll get a match on any string, but you'll only capture a blank string because of the question mark. This feature is much more useful when you have a more complicated regex. Here, the parser doesn't have to capture anything at all to get a match: the asterisk allows any number of characters in the capturing group, while the question mark makes the parser save as many as possible from the input text for later, resulting in nothing being captured.(.*)? captures a group zero or one times (?). That group consists of a run of any length (*) of any character (.). This also will match anything, but it will capture the first line, since the dot matches anything except a newline.\" represents double quotes*/
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Build a Stack Exchange Scraper
You are viewing a single comment's thread. Return to all comments →
Java 15