Regex to Make Text Links HTML Links

Regex used to select and replace URLs in plain text with clickable links



$regex = '(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.%-=#~\@!]*(\?\S+)?)?)?)';
$text = mb_ereg_replace($regex, '<a href="\\1">\\1</a>', $text);

Python Multiprocessing

The following code will enable you to implement multiprocessing in any Python project that contains a for loop. Take your for loop that has a long-running function in it and replace “for i in iterable” with your for loop conditions. Change “function_name” to the name of the function that will be executed. Change the list of function arguments to what you need.

import multiprocessing
number_cores = multiprocessing.cpu_count()
pool = multiprocessing.Pool(processes=number_cores)
r = [pool.apply_async(function_name, args=(function_arg1, function_arg2)) for i in iterable]
output = [p.get() for p in r]

The above code would be the multiprocessing implementation of the following.

output = []
for i in iterable:
    output += function_name(function_arg1, function_arg2)

Regex Sentence Splitter

Regex to match the end of sentences in order to split a block of text into sentences.


Python Code

sentence_regex = ur'([\.\?!][\'\"\u2018\u2019\u201c\u201d\)\]]*\s*(?<!\w\.\w.)(?<![A-Z][a-z][a-z]\.)(?<![A-Z][a-z]\.)(?<![A-Z]\.)\s+)'
regex = re.compile(sentence_regex, flags=re.UNICODE)
sentences = regex.split(TEXT_BLOCK)