fork(1) download
  1. object Main extends App {
  2. val line1 = "127.0.0.1 ssl.google-analytics.com"
  3. val line2 = "#127.0.0.1 ssl.google-analytics.com"
  4. val line3 = "127.0.0.1 ssl.google-analytics.com # Comment"
  5. val line4 = "#127.0.0.1 ssl.google-analytics.com # Comment"
  6.  
  7. val lineRegex = """(^#?).*(?:127\.0\.0\.1)\s+((?!-)[A-Za-z0-9-\.]{1,63}(?<!-)\.+[A-Za-z]{2,6})(?:\s+(#?.*))?""".r
  8. val m = lineRegex.findFirstMatchIn(line2).get
  9.  
  10. println(line2.substring(m.start(1), m.end(1))) // Should be # or ""
  11. println(line2.substring(m.start(2), m.end(2))) // Should be ssl.google-analytics.com
  12. if (m.group(3) != null) println(line2.substring(m.start(3), m.end(3))) // Should be # Comment or ""
  13. }
Success #stdin #stdout 0.37s 322432KB
stdin
Standard input is empty
stdout
#
ssl.google-analytics.com