XBMC v13.1 "Gotham" has been released! See Gotham FAQ for details.

RegEx tutorial

From XBMC
Jump to: navigation, search
Home icon grey.png   ▶ Development ▶ Add-on development ▶ Scrapers ▶ RegEx tutorial

This is meant to with your help become a general tutorial for using Regular Expression (commonly referred to as "RegEx" or "RegExp"), meant to be a more in depth guide to using XBMC, 'RegEx for dummies' so to speak.

Feel free to add anything to this guide.

These are a bunch of links to other RegEx tutorial which we can borrow information from for this guide:


Contents

1 Introduction to Regular Expression

Regular expressions are a tiny, highly specialized programming language. Regular expressions provide tools for developing complex pattern-matching and textual search-and-replace algorithms. Any PHP, Perl, egrep, awk, or sed developer will tell you that regular expressions are one of the most powerful utilities available for manipulating text and data. By creating patterns to match specific strings, a developer has total control over searching, extracting, or replacing data. In short, to master regular expressions is to master your data.

A regular expression is a series of characters that define a pattern. The pattern is then compared to a target string to see whether there are any matches to the pattern in the target string.

1.1 Patterns

Regular expressions are almost another language by itself. A pattern defines the criteria to search for within a string. Regular expressions can be as simple as plain text, or use a unique language consisting of special characters and modifiers to build these patterns.

1.1.1 Regex Examples (PCRE)

1.1.1.1 Regular expression examples for decimals input

Positive Integers --- ^\d+$
Negative Integers --- ^-\d+$
Integer --- ^-{0,1}\d+$
Positive Number --- ^\d*\.{0,1}\d+$
Negative Number --- ^-\d*\.{0,1}\d+$
Positive Number or Negative Number - ^-{0,1}\d*\.{0,1}\d+$
Phone number --- ^\+?[\d\s]{3,}$
Phone with code --- ^\+?[\d\s]+\(?[\d\s]{10,}$
Year 1900-2099 --- ^(19|20)[\d]{2,2}$
Date (dd mm yyyy, d/m/yyyy, etc.) --- ^([1-9]|0[1-9]|[12][0-9]|3[01])\D([1-9]|0[1-9]|1[012])\D(19[0-9][0-9]|20[0-9][0-9])$
IP v4 --- ^(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]){3}$

1.1.1.2 Regular expression examples for Alphabetic input

Personal Name --- ^[\w\.\']{2,}([\s][\w\.\']{2,})+$
Username --- ^[\w\d\_\.]{4,}$
Password at least 6 symbols --- ^.{6,}$
Password or empty input --- ^.{6,}$|^$
email --- ^[\_]*([a-z0-9]+(\.|\_*)?)+@([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$
domain --- ^([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$

1.1.1.3 Other regular expressions

Match no input --- ^$
Match blank input --- ^\s[\t]*$
Match New line --- [\r\n]|$

1.1.2 Forum Discussions

Attention talk.png Extra REGEX for TV Show Episode matching
Attention talk.png Regular Expressions

1.2 Third-party tools and utilities

Third-party tools and utilities can help you write or confirm and verify your written regexs.

Personal tools
Namespaces

Variants
Actions
Navigation
Wiki help
Toolbox