Changes

Jump to navigation Jump to search
1,019 bytes added ,  08:02, 14 November 2018
no edit summary
PCRE was originally written for the Exim MTA, but is now used by many high-profile open source projects, including Apache, PHP, KDE, Postfix, Analog, and Nmap
== Should I use perl, bash, php, awk, sed, ...? ==
PHP has rich regular expression support. Perl obviously does too. So when you're at the command line with BASH, what's the best way to quickly search some content for a pattern using a rich regular expression? It can be hard to use bash because of all the quoting and interpolation. But, let's look at a couple examples of searching a PHP configuration file for variable assignments.
Using perl, it's easy to print out only the parenthetical sub-expression
<source lang="bash">
perl -ne 'print $1 if /\$wgDBuser.*"(.*)"/' ./LocalSettings.php
</source>
Using grep, you have \K for variable length look-behind but it may not be available on older systems. Thus, you may need to use cut
<source lang="bash">
grep -Po '(?<=\$wgDBuser).*"(.*)"' ./LocalSettings.php | cut -d \" -f 2
</source>
== Resources ==
;Regex in Python (and [[Ansible]] since it's written in Python)
: https://docs.python.org/2.7/library/re.html
; Regex in Javascript
: http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:RegExp
; Regex on Windows
: http://weitz.de/regex-coach/
; RegExr.com for live testing of regex (uses GitHub login to save)
== See Also ==
4,558

edits

Navigation menu