Changes

Jump to: navigation, search

OPS102 - Regular Expressions

2,167 bytes added, 23:27, 5 December 2023
no edit summary
|Private IP Address||<code><nowiki>^(10\.((2[0-5][0-9]|[1-2][0-9][0-9]|[1-9][0-9]|[1-9]))|192\.168|172\.(1[6-9]|2[0-9]|3[0-1]))\.(2[0-5][0-9]|[1-2][0-9][0-9]|[1-9][0-9]|[1-9])\.(2[0-5][0-9]|[1-2][0-9][0-9]|[1-9][0-9]|[1-9])</nowiki></code>||10.4.72.13<br>172.16.97.1<br>192.168.0.1||IP=192.168.113.42<br>1.1.1.1<br>4.4.8.8<br>192.169.12.6<br>192.168.400.37<br>Address is 1 . 2 . 3 . 4||Valid IPv4 dotted quad address with a first octet of 10; or first two octets of 192.168; or first octet of 172 followed by a second octet in the range 16-31.
|}
 
== Regular Expression Dialects ==
 
Regular expressions have evolved over the years, and the various tools that handle regular expressions have different capabilities and slightly different syntax.
 
In particular, the original Unix search tool <code>grep</code> came in three varieties:
* fgrep, which could search only for fixed text patterns
* grep, which handled basic regular expressions
* egrep, which handled an extended form of regular expressions
 
The GNU project originally shipped all three commands, but fgrep and egrep were never fully standardized, so they were removed from the Posix standard in 2001. They were recently also removed from the GNU project.
 
Unlike the original Unix grep, the GNU grep can handle the full extended regular expression syntax, in either of two ways:
* To use the special characters (called "meta-characters") ?, +, {, |, (, and ) preceed them with a backslash. In other words, while a backslash makes special characters like . or * ''ordinary'', it also makes ''ordinary'' characters ? + { | } into special characters.
* Alternately, use the <code>-E</code> option to make grep understand extended regular expressions, which causes ? + { ( | ) to become special characters.
 
Other tools, such as sed, similarly require backslashes in front of some of the extended regexp meta-characters.
 
The Perl language introduced one of the most powerful and consistent versions of the regular expression language. There has been increasing consensus around "Perl-Compatible Regular Extensions" (aka PCRE) and that dialect is available in many tools (including GNU grep via the <code>-P</code> option, as well as the [https://www.pcre.org/ PCRE/PCRE2 library] for C and C++ programs, which is used in many software packages including Safari and Apache).
 
== Using Regular Expressions ==
 
Regular expressions can be used in many places:
 
* Linux
** GNU grep
** The less command, using the / and ? keystrokes for searching forward and backward
** The vi/vim editor
** The bash test command <code><nowiki>[[ "string" =~ regexp ]]</nowiki></code>
 
* Windows
** findstr /R
 
* Languages
** Python
** JavaScript
** Perl
** ...and many others

Navigation menu