Regular Expressions: Difference between revisions

No edit summary
insert image
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[File:Email-regex.svg|thumb]]
Regex is short for Regular Expression and is a syntax that allows for powerful pattern matching.
Regex is short for Regular Expression and is a syntax that allows for powerful pattern matching.


Line 4: Line 5:


== Multiline Edits ==
== Multiline Edits ==
Most graphical text editors or word processors have a single line input for the Search/Replace dialog.  This is unsuitable for many text edit situations where the string you're looking to replace spans multiple lines.
Most graphical text editors or word processors have a single line input for the Search/Replace dialog.  This is unsuitable for many text edit situations where the string you're looking to replace spans multiple lines. When using [[VSCode]], although the search and replace dialog is single line, it will dynamically expand to multiple lines if you COPY/PASTE multiple lines into the 'find' portion. You can also use the 'regex' option and line-terminator escapes in your pattern (e.g. "this \n will \n search across \n multiple lines").
 
'''Quanta''' integrates '''KFileReplace''', which you can launch standalone or use within Quanta to do multiline regex-capable search and replace.  Since the 'Advanced Search/Replace' dialog does not allow multiline text input, it's a bit awkward to do this from Quanta.  However, once the KFileReplace part is open in Quanta, you can edit your search session any way you like including entering multiple line text as the search 'needle'.
 
To launch KFileReplace standalone, press the 'Alt + F2' keys and type 'KFileReplace' (enter)


== Single Line ==
== Single Line ==
Line 23: Line 20:
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
<source lang="bash">
<syntaxhighlight lang="bash">
perl -ne 'print $1 if /\$wgDBuser.*"(.*)"/' ./LocalSettings.php
perl -ne 'print $1 if /\$wgDBuser.*"(.*)"/' ./LocalSettings.php
</source>
</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
<source lang="bash">
<syntaxhighlight lang="bash">
grep -Po '(?<=\$wgDBuser).*"(.*)"' ./LocalSettings.php | cut -d \" -f 2
grep -Po '(?<=\$wgDBuser).*"(.*)"' ./LocalSettings.php | cut -d \" -f 2
</source>
</syntaxhighlight>


== Resources ==
== Resources ==