Register now and start sharing your code snippets.
-->

List all links in a file with Ruby and regular expressions

Ruby posted about 1 year ago by christian

This snippet lists all text links:

   1  data = File.read('the_link_collection.txt')
   2  
   3  links = data.scan /href="([^"]*)[^>]*>([^<]*)<\/a>/im
   4  
   5  links.each do |link|
   6    puts "#{link[1].chomp} = #{link[0]}"
   7  end

Tagged ruby, regex

Split a URL into protocol, domain, port and URI using regular expressions

Java posted about 1 year ago by christian

   1  // Split URL into protocol, domain, port and URI
   2  Pattern pattern = Pattern.compile("(https?://)([^:^/]*)(:\\d*)?(.*)?");
   3  Matcher matcher = pattern.matcher(url);
   4  
   5  matcher.find();
   6  
   7  String protocol = matcher.group(1);            
   8  String domain   = matcher.group(2);
   9  String port     = matcher.group(3);
  10  String uri      = matcher.group(4);

Tagged regex, java, url