preg_match and UTF-8 in PHP

Although the u modifier makes both the pattern and subject be interpreted as UTF-8, the captured offsets are still counted in bytes. You can use mb_strlen to get the length in UTF-8 characters rather than bytes: $str = “\xC2\xA1Hola!”; preg_match(‘/H/u’, $str, $a_matches, PREG_OFFSET_CAPTURE); echo mb_strlen(substr($str, 0, $a_matches[0][1]));

Unicode Regex; Invalid XML characters

I know this isn’t exactly an answer to your question, but it’s helpful to have it here: Regular Expression to match valid XML Characters: [\u0009\u000a\u000d\u0020-\uD7FF\uE000-\uFFFD] So to remove invalid chars from XML, you’d do something like // filters control characters but allows only properly-formed surrogate sequences private static Regex _invalidXMLChars = new Regex( @”(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F\uFEFF\uFFFE\uFFFF]”, RegexOptions.Compiled); … Read more

Match correct addition of two binary numbers with PCRE regex

TL;DR: Yes, it indeed is possible (use with Jx flags): (?(DEFINE) (?<add> \s*\+\s* ) (?<eq> \s*=\s* ) (?<carry> (?(cl)(?(cr)|\d0)|(?(cr)\d0|(*F))) ) (?<digitadd> (?(?= (?(?= (?(l1)(?(r1)|(*F))|(?(r1)(*F))) )(?&carry)|(?!(?&carry))) )1|0) ) (?<recursedigit> (?&add) 0*+ (?:\d*(?:0|1(?<r1>)))? (?(ro)|(?=(?<cr>1)?))\k<r> (?&eq) \d*(?&digitadd)\k<f>\b | (?=\d* (?&add) 0*+ (?:\k<r>(?<ro>)|\d*(?<r>\d\k<r>)) (?&eq) \d*(?<f>\d\k<f>)\b) \d(?&recursedigit) ) (?<checkdigit> (?:0|1(?<l1>)) (?=(?<cl>1)?) (?<r>) (?<f>) (?&recursedigit) ) (?<carryoverflow> (?<x>\d+) 0 (?<y> \k<r> … Read more

How to use non-capturing groups in grep?

“Non-capturing” doesn’t mean that the group isn’t part of the match; it means that the group’s value isn’t saved for use in back-references. What you are looking for is a look-behind zero-width assertion: grep -Po “(?<=syntaxHighlighterConfig\.)[a-zA-Z]+Color” file

Tilde operator in Regular expressions

In this case, it’s just being used as a delimiter. Generally, in PHP, the first and last characters of a regular expression are “delimiters” to mark the start and ending position of a matching portion (in case you want to add modifiers at the end, like ungreedy, etc) Generally PHP works this out from the … Read more

Rebuild uwsgi with pcre support

pip install uwsgi -I Won’t recompile the uwsgi binary, it just reinstalls the python egg. You need to rebuild the uwsgi binary with the pcre libraries. sudo apt-get install libpcre3 libpcre3-dev I think the easiest way is just to uninstall uwsgi and then run the pip installer again. pip uninstall uwsgi sudo apt-get remove uwsgi … Read more