Difference between revisions of "Regex"
(12 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
==regex== | ==regex== | ||
to take out all non alpha characters | https://alligator.io/js/regular-expressions-for-regular-people/ | ||
http://exploringjs.com/impatient-js/ch_regular-expressions.html | |||
https://www.smashingmagazine.com/2019/02/regexp-features-regular-expressions/ | |||
https://regex101.com/ | |||
===php example=== | |||
$search = array( | |||
'@<script[^>]*?>.*?</script>@si', // Strip out javascript | |||
'@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags | |||
'@<style[^>]*?>.*?</style>@siU', // Strip style tags properly | |||
'@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments | |||
); | |||
$output = preg_replace($search, '', $input); | |||
return $output; | |||
} | |||
?> | |||
<?php | |||
function sanitize($input) { | |||
if (is_array($input)) { | |||
foreach($input as $var=>$val) { | |||
$output[$var] = sanitize($val); | |||
} | |||
} | |||
else { | |||
if (get_magic_quotes_gpc()) { | |||
$input = stripslashes($input); | |||
} | |||
$input = cleanInput($input); | |||
$output = mysql_real_escape_string($input); | |||
} | |||
return $output; | |||
} | |||
===typical=== | |||
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 | |||
replace spaces and punctuation with nothing str.replace(/\W/g, '') | |||
: \W not letters digits or underscores | |||
to take out all non alpha characters (Ruby) | |||
<pre> | |||
test_str = "Madam, I'm Adam" | test_str = "Madam, I'm Adam" | ||
str = test_str.gsub(/[^a-zA-Z]/,'') | str = test_str.gsub(/[^a-zA-Z]/,'') | ||
puts str | puts str | ||
returns MadamImAdam | returns MadamImAdam | ||
</pre> | |||
*^[ \t]+ //finds all the space and tabs | *^[ \t]+ //finds all the space and tabs | ||
Line 18: | Line 98: | ||
*http://www.grymoire.com/Unix/Regular.html#uh-2 | *http://www.grymoire.com/Unix/Regular.html#uh-2 | ||
*http://www.regular-expressions.info/php.html | *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=== | ===expressions=== | ||
If you want to get'Aloha World'out | If you want to get'Aloha World'out |
Latest revision as of 14:05, 8 February 2019
regex
https://alligator.io/js/regular-expressions-for-regular-people/
http://exploringjs.com/impatient-js/ch_regular-expressions.html
https://www.smashingmagazine.com/2019/02/regexp-features-regular-expressions/
php example
$search = array( '@<script[^>]*?>.*?</script>@si', // Strip out javascript '@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags '@<style[^>]*?>.*?</style>@siU', // Strip style tags properly '@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments );
$output = preg_replace($search, , $input); return $output; }
?> <?php function sanitize($input) {
if (is_array($input)) { foreach($input as $var=>$val) { $output[$var] = sanitize($val); } } else { if (get_magic_quotes_gpc()) { $input = stripslashes($input); } $input = cleanInput($input); $output = mysql_real_escape_string($input); } return $output;
}
typical
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
replace spaces and punctuation with nothing str.replace(/\W/g, )
- \W not letters digits or underscores
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:
RegEx:
\<div\sclass\=\"somename\"\>(?<Text>.*?)\<\/div\>
Yields:
Aloha World (note: In a single group named Text)