Skip to content

Instantly share code, notes, and snippets.

@ROMVoid95
Created March 8, 2021 07:31
Show Gist options
  • Save ROMVoid95/81fbaefb364e1a8fd6ea3e24994eb1c3 to your computer and use it in GitHub Desktop.
Save ROMVoid95/81fbaefb364e1a8fd6ea3e24994eb1c3 to your computer and use it in GitHub Desktop.
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
static final String protocol = "(?<protocol>(?:https?:\\/\\/?)+(?:www(?!.))?)?";
static final String tdl = "(?<tdl>[a-zA-Z0-9@:._-]{2,256}\\.[a-z0-9]{2,6}(?:[:0-9]{1,5})?)";
static final String path = "(?<path>(?:\\/)[-a-zA-Z0-9\\/_]+)?";
static final String params = "(?<params>[-a-zA-Z0-9?%#+()=&]+)?";
static Map<Integer, String> namedGroups = new HashMap<>();
public static void main(String[] args) throws Exception {
Set<String> set = new TreeSet<>();
set.add("http://foo.com/blah_blah");
set.add("http://foo.com/blah_blah/");
set.add("https://www.example.com/wpstyle/?p=364");
set.add("https://www.example.com/wpstyle/wpath/?p=364&?param=this");
set.add("https://www.example.com/foo/?bar=baz&inga=42&quux");
set.add("google.com");
set.add("www.google.com");
set.add("http://foo.com:");
set.add("http://foo.com:8080/blah_blah");
set.add("https://www.example.com/foo/?bar=baz&inga=42&quux");
int matchCount = 0;
for (String element : set) {
matchCount = matchCount + 1;
runRegex(element, matchCount);
}
}
private static void runRegex(String string, int matchCount) {
Pattern p = Pattern.compile(protocol + tdl + path + params);
setNamedGroupCandidates(p.toString());
Matcher m = p.matcher(string);
if (m.find()) {
// Remove invalid groups
Iterator<String> i = namedGroups.values().iterator();
while (i.hasNext()) {
try {
m.group(i.next());
} catch (IllegalArgumentException e) {
i.remove();
}
}
System.out.println("Match " + matchCount + ":");
System.out.println();
printMatches(m, namedGroups.values());
}
}
private static void printMatches(Matcher matcher, Collection<String> namedGroups) {
for (String name: namedGroups) {
String matchedString = matcher.group(name);
if (matchedString != null) {
System.out.println(name + ": " + matchedString);
}
}
System.out.println();
System.out.println("------------------------");
}
private static void setNamedGroupCandidates(String regex) {
Matcher m = Pattern.compile("\\(\\?<([a-zA-Z][a-zA-Z0-9]*)>").matcher(regex);
int order = 0;
while (m.find()) {
order += 1;
namedGroups.put(order, m.group(1));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment