Difference between revisions of "Regex"
From Wiki2
(→regex) |
|||
Line 1: | Line 1: | ||
==regex== | ==regex== | ||
A username fro 3- 16 characters w -or_ /^[a-z0-9_-]{3,16}$/ | |||
a password 6-18 long /^[a-z0-9_-]{6,18}$/ | |||
a hex number /^#?([a-f0-9]{6}|[a-f0-9]{3})$/ | |||
:a number sign is optional because it is followed a question mark but if there takes it(greedy) | |||
:6 characters or 3 character | |||
a slug /^[a-z0-9-]+$/ | |||
:beginning of the string (^), followed by one or more (the plus sign) letters, numbers, or hyphens. and the end of the string ($). | |||
an email /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/ | |||
: . get delimited \. | |||
: each section in parens | |||
only lower case, nubers or dots'/[^a-z0-9.]/' | only lower case, nubers or dots'/[^a-z0-9.]/' | ||
url /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/ | |||
:s? s if you got em | |||
: delimit / and . with \ | |||
: you don't need http at all (https?:\/\/)? but be greedy | |||
: \d is any number | |||
: ([\da-z\.-]+) numbers letters dotts or hyphens | |||
: \.([a-z\.]{2,6})~ .com .co .commie | |||
: ([\/\w \.-]*)*\/?$/ files and directories | |||
: ([\/\w \.-]*) * is zero or more, \w is words | |||
html tag /^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/ | |||
: .* any characters | |||
: ([^<]+) any characters but a < | |||
: (?:>(.*)<\/\1>|\s+\/>) non-capture group.it will contain either a greater than sign, some content, and a closing tag; or some spaces, a forward slash, and a greater than sign \1 represents the content that was captured in the first capturing group | |||
no dots: ^[^\.]*$ | no dots: ^[^\.]*$ | ||
^ - beginning of string | ^ - beginning of string | ||
[^\.]* - any character except ., any number of repetitions | [^\.]* - any character except ., any number of repetitions | ||
$ - end of string | $ - end of string | ||
to take out all non alpha characters (Ruby) | to take out all non alpha characters (Ruby) |
Revision as of 10:55, 3 March 2015
regex
A username fro 3- 16 characters w -or_ /^[a-z0-9_-]{3,16}$/
a password 6-18 long /^[a-z0-9_-]{6,18}$/
a hex number /^#?([a-f0-9]{6}|[a-f0-9]{3})$/
- a number sign is optional because it is followed a question mark but if there takes it(greedy)
- 6 characters or 3 character
a slug /^[a-z0-9-]+$/
- beginning of the string (^), followed by one or more (the plus sign) letters, numbers, or hyphens. and the end of the string ($).
an email /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/
- . get delimited \.
- each section in parens
only lower case, nubers or dots'/[^a-z0-9.]/'
url /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
- s? s if you got em
- delimit / and . with \
- you don't need http at all (https?:\/\/)? but be greedy
- \d is any number
- ([\da-z\.-]+) numbers letters dotts or hyphens
- \.([a-z\.]{2,6})~ .com .co .commie
- ([\/\w \.-]*)*\/?$/ files and directories
- ([\/\w \.-]*) * is zero or more, \w is words
html tag /^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/
- .* any characters
- ([^<]+) any characters but a <
- (?:>(.*)<\/\1>|\s+\/>) non-capture group.it will contain either a greater than sign, some content, and a closing tag; or some spaces, a forward slash, and a greater than sign \1 represents the content that was captured in the first capturing group
no dots: ^[^\.]*$
^ - beginning of string [^\.]* - any character except ., any number of repetitions $ - end of string
to take out all non alpha characters (Ruby)
test_str = "Madam, I'm Adam" str = test_str.gsub(/[^a-zA-Z]/,'') puts str returns MadamImAdam
- ^[ \t]+ //finds all the space and tabs
- [1-9]\. //finds all the line numbers. (replace with #)
- [A-E]\. //find A. etc (replace with ##)
to clean special characters from a string
$clean = preg_replace("/^[^a-z0-9]?(.*?)[^a-z0-9]?$/i", "$1", $text);
sites
- http://rubular.com
- http://e-texteditor.com/blog/2010/beyond-vi
- http://www.grymoire.com/Unix/Regular.html#uh-2
- http://www.regular-expressions.info/php.html
- http://www.bluebox.net/about/blog/2013/02/using-regular-expressions-in-ruby-part-1-of-3/
expressions
If you want to get'Aloha World'out
Input:
Hello World
Aloha World
Hey There
RegEx:
\<div\sclass\=\"somename\"\>(?<Text>.*?)\<\/div\>
Yields:
Aloha World (note: In a single group named Text)