1. Jason R. Coombs
  2. stackoverflow

Commits

jaraco  committed 951ef55

adding new answer

  • Participants
  • Parent commits 81adec5
  • Branches stackoverflow

Comments (0)

Files changed (2)

File 2489731/make_subs.py

View file
  • Ignore whitespace
+    import re
+    import sys
+
+    def href_repl(matcher):
+        "replace the matched URL with a hyperlink"
+        # here you could analyze the URL further and make exceptions, etc
+        #  to how you did the substitution. For now, do a simple
+        #  substitution.
+        href = matcher.group(0)
+        return '<a href="{href}">{href}</a>'.format(**vars())
+
+    text = open(sys.argv[1]).read()
+    url_pattern = re.compile(re.escape('http://') + '[^ ]*')
+    sys.stdout.write(url_pattern.sub(href_repl, text))

File 2489731/yourfile.txt

View file
  • Ignore whitespace
+This is my http://www.website.com .
+
+You can reach me at email@example.com
+