Questions about this topic? Sign up to ask in the talk tab.

Difference between revisions of "Regular expressions"

From NetSec
Jump to: navigation, search
(Literals)
(Specials)
Line 8: Line 8:
 
Similarly, a regex search for <code>hat</code> in the string <code>Blackhat Academy</code> will return 'hat' from the end of the first word. This is merely a string of literal characters, and the regex engine handles it the same way as it handles a single literal character.
 
Similarly, a regex search for <code>hat</code> in the string <code>Blackhat Academy</code> will return 'hat' from the end of the first word. This is merely a string of literal characters, and the regex engine handles it the same way as it handles a single literal character.
  
====Specials====
+
====Metacharacters====
 +
Regex wouldn't really be that useful if we just wanted to match literal strings of text. That's where special characters come in to play. Each metacharacter has it's own use, and in order to use any of them as literal characters, you need to escape them using the <code>\</code> (backslash) key.
 +
 
 +
There are 11 metacharacters we'll use:
 +
*Opening square bracket <code>[</code>
 +
*Backslash <code>\</code>
 +
*Caret <code>^</code>
 +
*Dollar Sign <code>$</code>
 +
*Dot <code>.</code>
 +
*Pipe <code>|</code>
 +
*Question mark <code>?</code>
 +
*Asterik <code>*</code>
 +
*Plus <code>+</code>
 +
*Open round bracket <code>(</code>
 +
*Close round bracket <code>)</code>
 +
 
 +
Take <code>1+1=2</code> for example. The correct regex to match this string is <code>1\+1=2</code>. It's important to note that <code>1+1=2</code> is still valid regex, but it wouldn't match <code>1+1=2</code>. Instead, it would match <code>111=2</code> in the string <code>111=25*4+11</code>. This is the a great example of how to properly escape special characters when you want to find their literal character counterpart, but how it might not always throw an error, even if you don't escape your special characters.
 +
 
 
====Non-Printable====
 
====Non-Printable====
 
===Character Classes (Sets)===
 
===Character Classes (Sets)===

Revision as of 00:54, 1 July 2012

This article contains too little information, it should be expanded or updated.
Things you can do to help:
  • add more content.
  • update current content.

Regular Expressions (regex) are essentially a search engine for finding patterns in a text. While the syntax is a bit tricky to learn, regex will save tons of time and effort in the long run. Many of you are probably familiar with regex, even if only through the use of wildcards. Wildcard notation, such as *.html, matches to all html files in the given search directory. Regex takes this idea and expands on it dramatically, allowing for very complicated search patterns. A regular expression to find all html files in a given directory would be .*\.html$

Syntax

Characters

Literals

The most basic regex is a literal character. A literal character, such as a matches a in the string alex. However, in a string such as adam, it will only match the first a, before the 'd', unless you tell the regex engine otherwise. Most text editors that have a 'find' function, also have a 'find next' function.

Similarly, a regex search for hat in the string Blackhat Academy will return 'hat' from the end of the first word. This is merely a string of literal characters, and the regex engine handles it the same way as it handles a single literal character.

Metacharacters

Regex wouldn't really be that useful if we just wanted to match literal strings of text. That's where special characters come in to play. Each metacharacter has it's own use, and in order to use any of them as literal characters, you need to escape them using the \ (backslash) key.

There are 11 metacharacters we'll use:

  • Opening square bracket [
  • Backslash \
  • Caret ^
  • Dollar Sign $
  • Dot .
  • Pipe |
  • Question mark ?
  • Asterik *
  • Plus +
  • Open round bracket (
  • Close round bracket )

Take 1+1=2 for example. The correct regex to match this string is 1\+1=2. It's important to note that 1+1=2 is still valid regex, but it wouldn't match 1+1=2. Instead, it would match 111=2 in the string 111=25*4+11. This is the a great example of how to properly escape special characters when you want to find their literal character counterpart, but how it might not always throw an error, even if you don't escape your special characters.

Non-Printable

Character Classes (Sets)

Negated Character Classes

Metacharacters

Shorthand

Negated Shorthand

Repeating Character Classes

Dot

Anchors

Word Boundaries

Alternation

Quantifiers

Tools

Utilities

Programming Languages

  • Gnulib
  • Java
  • JavaScript
  • .NET
  • Perl
  • PHP
  • PowerShell
  • Python
  • Ruby

Databases