Latest 100 public
snipts » regex
showing 1-20 of 49 snipts for regex
-
∞ Regex remove img tag
$content = preg_replace("/<img[^>]+\>/i", "", $content); -
∞ Perl remove html tags
#!/usr/bin/perl while (<STDIN>) { s/<[a-zA-Z\/][^>]*>//g; print $_; }
-
∞ Regular expression in PHP to convert URLs to HTML links
<?php $pattern = "@\b(https?://)?(([0-9a-zA-Z_!~*'().&=+$%-]+:)?[0-9a-zA-Z_!~*'().&=+$%-]+\@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-zA-Z_!~*'()-]+\.)*([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,6})(:[0-9]{1,4})?((/[0-9a-zA-Z_!~*'().;?:\@&=+$,%#-]+)*/?)@"; $text = preg_replace($pattern, '<a target="_blank" rel="nofollow" href="\0">\0</a>', $text); return $text;
-
∞ Integer from String (dollar formatted)
function extractNumber(str) { str = str.match(/\d+/g); return str.join(''); } $(function() { // Sets a ele based on a number of values in the format like ' $15.50/Month' var total = 0; $('p').not('#total').each(function() { total += parseInt( extractNumber( $.trim( $(this).text() ) ) ); }); total = '$' + Math.round(total)/100;// make price to 2 decimal places $('#total').text(total); });
-
∞ Email validation for preg_match()
/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i -
∞ PHP email regex check
<?php $email = "someone@example.com"; if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) { echo "Valid email address."; } else { echo "Invalid email address."; } ?>
-
∞ Email validation using preg_match (via James Watts and Francisco Jose Martin Moreno)
/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i -
∞ get to_date regex for notepad++
#Original regex to_date\('[0-9]{2}-[0-9]{2}-[0-9]{4}\s{1}[0-9]{2}:[0-9]{2}:[0-9]{2}',\s{1}'dd-mm-yyyy\s{1}hh24:mi:ss'\) to_date\('[0-9]{2}-[A-Z]{3}-[0-9]{2}','DD-MON-RR'\) # For notepad++ to_date\('[0-9]+-[0-9]+-[0-9]+\s[0-9]+:[0-9]+:[0-9]+',\s'dd-mm-yyyy\shh24:mi:ss'\) to_date\('[0-9]+-[A-Z]+-[0-9]+','DD-MON-RR'\)
-
∞ Email RegExp
var pattern:RegExp = (\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6}); // PHP return preg_match('/^[A-Za-z0-9\._\-+]+@[A-Za-z0-9_\-+]+(\.[A-Za-z0-9_\-+]+)+$/', $value);
-
∞ Is GIF, JPG, PNG
var pattern:RegExp = ([^\s]+(?=\.(jpg|gif|png))\.\2);
-
∞ RegEx - Find and Replace
var str:String = "Hello World!"; var myPattern:RegExp = /Hello/g; //regex to remove all hellos str = str.replace(myPattern, "Goodbye"); trace(str); // Goodbye World!
-
∞ Some useful regex...
// Validate URL /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \?=.-]*)*\/?$/ // Unclosed image tags <img([^>]+)(\s*[^\/])>
-
∞ jQuery valid email regex
$(function(){ var email = $("input#email").val(); if(isValidEmailAddress(email)) { $(email).after("hooray!"); } else { $(email).addClass("yourerror").after("<label class='error'>Email is not valid!</label>").focus(); return false; } }); function isValidEmailAddress(emailAddress) { var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i); return pattern.test(emailAddress); }
-
∞ Regular Expressions
//validate url /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \?=.-]*)*\/?$/ //Validate US phone number /^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/ //Test if a password is strong (?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$ //Validate US zip code ^[0-9]{5}(-[0-9]{4})?$ //Validate Canadian postal code ^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$ //Grab unclosed img tags <img([^>]+)(\s*[^\/])> //Find all CSS attributes \s(?[a-zA-Z-]+)\s[:]{1}\s*(?[a-zA-Z0-9\s.#]+)[;]{1} //Get code within <?php and ?> <\?[php]*([^\?>]*)\?>
-
∞ Dates Samples
<?php $currDate = date('Y-m-d h:i:s'); // convert timestamp from mysql $nextReviewDt = date('F d, Y',strtotime($nextReviewDt)); // Add 30 days $CommentStartDt = date('m/d/Y'); $CommentEndDt = date('m/d/Y', mktime(0,0,0,date("m"),date("d")+30,date("Y"))); // add 30 days // convert year to 4 digits if ($y > 60) $y = '19'.$y; else $y = '20'.$y; // split if there are slash or dash list($m, $d, $y) = split('[/|-]', $iDate); if (preg_match("/-/", $iDate)) list($m, $d, $y) = split('-', $iDate); else if (preg_match("/\//", $iDate)) list($m, $d, $y) = split('/', $iDate); $specDate = mktime(0, 0, 0, $m, $d, $y); $numOfDays = (time() - $specDate) / (24 * 60 * 60); // convert date from mysql db if (preg_match("/(.*)Dt$/", $dbDate)) $dbDate = date("M d, Y h:ia", strtotime($dbDate)); // output dates - walk the days $startDt = '2009-01-04'; $endDt = date('Y-m-d'); while ($startDt != $endDt) { echo "$startDt <br/>"; // increment the day $startDt = strtotime(date("Y-m-d", strtotime($startDt)) . " +1 day"); // convert back to readable format $startDt = date("Y-m-d",$startDt); } function count_days( $a, $b ) { // First we need to break these dates into their constituent parts: $gd_a = getdate( $a ); $gd_b = getdate( $b ); // Now recreate these timestamps, based upon noon on each day // The specific time doesn't matter but it must be the same each day $a_new = mktime( 12, 0, 0, $gd_a['mon'], $gd_a['mday'], $gd_a['year'] ); $b_new = mktime( 12, 0, 0, $gd_b['mon'], $gd_b['mday'], $gd_b['year'] ); // Subtract these two numbers and divide by the number of seconds in a // day. Round the result since crossing over a daylight savings time // barrier will cause this time to be off by an hour or two. return round( abs( $a_new - $b_new ) / 86400 ); }
-
∞ Regex for static tag replacement.
re.sub("<p>\s*? \s*?</p>", "REPLACEMENT", string)
-
∞ Java regex tester
import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexTester { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String regex = "/ajax/\\w+/(.*)"; String[] strings = { "/ajax/hello/KID/de-mt.vc.ca02.06.04/1559286", "/ajax/hell_o/KID/de-mt.vc.ca02.06.04/1559286", "/ajax/h0e-l8l_o/KID/de-mt.vc.ca02.06.04/1559286", }; for (String s : strings) { Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(s); String kid = ""; if (matcher.find()) { // group(0) returns the entire match, not a captured group kid = matcher.group(1); } System.out.println(kid); } } }
-
∞ Replace any whitespace with only a single space
function replace_whitespace($Value = '') { return preg_replace('/\s+/', ' ', trim($Value)); }
-
∞ use double brackets for compound and regex test
# Use Double Brackets for Compound and Regex Tests http://hacktux.com/bash/script/efficient The [ or test builtins can be used to test expressions, but the [[ builtin operator additionally provides compound commands and regular expression matching. if [[ expression1 || expression2 ]]; then do_something; fi if [[ string =~ regex ]]; then do_something; fi
-
∞ URL Regex
\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))


