Archive:RegEx tutorial: Difference between revisions

From Official Kodi Wiki
Jump to navigation Jump to search
>Gamester17
No edit summary
No edit summary
(25 intermediate revisions by 8 users not shown)
Line 1: Line 1:
This is meant to become a general tutorial for using Regular Expression (RegEx), meant to be a more in depth guide to using XBMC, 'RegEx for dummies' so to speak.
{{mininav|[[Development]]|[[Add-on development]]|[[Scrapers]]}}
 
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 Kodi, 'RegEx for dummies' so to speak.


Feel free to add anything to this guide.
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:
These are a bunch of links to other RegEx tutorial which we can borrow information from for this guide:
* [http://www.smashingmagazine.com/2009/06/01/essential-guide-to-regular-expressions-tools-tutorials-and-resources/ Essential Guide To Regular Expressions: Tools and Tutorials]
* [http://forum.kodi.tv/showthread.php?tid=25349&pid=128866#pid128866 Kodi developer explanation on RegEx] (Regular Expressions Forum topic)
* http://en.wikipedia.org/wiki/Regex
* http://en.wikipedia.org/wiki/Regex
* http://www.english.uga.edu/humcomp/perl/regex2a.html
* http://www.regular-expressions.info/tutorial.html
* http://www.codeproject.com/dotnet/RegexTutorial.asp
* http://www.codeproject.com/dotnet/RegexTutorial.asp


=Introduction to Regular Expression=
 
= 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.  
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.
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.


==Patterns==
== 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.
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.


===Regex Examples (PCRE)===
=== Regex Examples (PCRE) ===
====Regular expression examples for decimals input====
==== Regular expression examples for decimals input ====
  Positive Integers --- ^\d+$
  Positive Integers --- ^\d+$
  Negative Integers --- ^-\d+$
  Negative Integers --- ^-\d+$
Line 30: Line 36:
  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}$
  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}$


====Regular expression examples for Alphabetic input====
==== Regular expression examples for Alphabetic input ====
  Personal Name --- ^[\w\.\']{2,}([\s][\w\.\']{2,})+$
  Personal Name --- ^[\w\.\']{2,}([\s][\w\.\']{2,})+$
  Username --- ^[\w\d\_\.]{4,}$
  Username --- ^[\w\d\_\.]{4,}$
Line 38: Line 44:
  domain --- ^([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$
  domain --- ^([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$


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


=== Forum Discussions ===
{{forum link2|[[forum:51614|Extra REGEX for TV Show Episode matching]]}}
{{forum link2|[[forum:25349|Regular Expressions]]}}


 
== Third-party tools and utilities ==
==Third-party tools and utilities==
Third-party tools and utilities can help you write or confirm and verify your written regexs.
Third-party tools and utilities can help you write or confirm and verify your written regexs.
* [http://www.regextester.com REGex TESTER] - webbased tested
* [http://www.regextester.com/index2.html REGex TESTER] - web-based tested
* [http://www.quanetic.com/regex.php Regular Expression Tester] - another web-based tested
* [http://notepad-plus.sourceforge.net Notepad++ for Windows]
* [http://notepad-plus.sourceforge.net Notepad++ for Windows]
* [http://macromates.com TextMate for Mac OSX]
* [http://macromates.com TextMate for Mac OSX]


 
[[Category:How-to]]
 
[[Category:Development]]
[[category:How To|Scraper]]
[[Category:Scraper]]
[[category:Development]]
[[Category:Python]]
[[category:Inner Workings]]
[[category:Scraper]]

Revision as of 20:49, 19 May 2015

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 Kodi, '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:


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.

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.

Regex Examples (PCRE)

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}$

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}$

Other regular expressions

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

Forum Discussions

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

Third-party tools and utilities

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