Regular Expressions: Difference between revisions
No edit summary |
m Text replacement - "<(\/?)source" to "<$1syntaxhighlight" |
||
| Line 23: | Line 23: | ||
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. | 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 | Using perl, it's easy to print out only the parenthetical sub-expression | ||
< | <syntaxhighlight lang="bash"> | ||
perl -ne 'print $1 if /\$wgDBuser.*"(.*)"/' ./LocalSettings.php | perl -ne 'print $1 if /\$wgDBuser.*"(.*)"/' ./LocalSettings.php | ||
</ | </syntaxhighlight> | ||
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 | 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 | ||
< | <syntaxhighlight lang="bash"> | ||
grep -Po '(?<=\$wgDBuser).*"(.*)"' ./LocalSettings.php | cut -d \" -f 2 | grep -Po '(?<=\$wgDBuser).*"(.*)"' ./LocalSettings.php | cut -d \" -f 2 | ||
</ | </syntaxhighlight> | ||
== Resources == | == Resources == | ||