Back · Index

Regex

I love regex and I hate regex at the same time.

I certainly do not know it on a good enough level, but my adventures with creating and developing things with PHP have certainly helped strengthen the "relationship" I have with regex. It's a necessary evil tool.

ITT are bits, pieces and various patterns that might be useful for me and others.

Text Summarization Experiment

Word Pairings

# Finds one word before and after keyword in order to find frequent pairings as well.
preg_match_all("/(?i)(\w+)\W+$word\W+(\w+)/", $data, $matches, PREG_SET_ORDER); /* find all pairings */

Find all words

$pattern = "/(\b[a-zA-Z'\-]+\b)/"; /* find all words not including numbers or punctuation */

Saisho

YAML Front Matter Header

# This is not the best way to say the least. Splitting the text by "---" is probably better.
preg_match("'^---(.+?)---'si", $str, $yaml); # Get header
preg_match_all("'(\w+):\s?(.+)'m", $yaml, $yaml_attribs, PREG_SET_ORDER); # Get attributes
Loaded in 2.596ms