Public
snipts » regex
showing 1-20 of 34 snipts for regex
-
∞ 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]|/)))
-
∞ multiple regex replacement from a single command
sed -e 's/root/r00t/;s/regular/not/' testing -
∞ CSS string parser for grabbing all 4 border values
var colorRegex = /#[1-90a-f]+/gi; //hex values only var borderColor = cssParse(opts.borderColor,colorRegex); var widthRegex = /[0-9]+/g; var borderWidth = cssParse(opts.borderWidth,widthRegex); function cssParse(css,reg){ //parses string into separate values for each side which is required for color anim and other uses var temp = css.match(reg),rtn = [],l = temp.length; if (l > 1) { rtn[0] = temp[0]; rtn[1] = temp[1]; rtn[2] = (l == 2) ? temp[0] : temp[2]; rtn[3] = (l == 4) ? temp[3] : temp[1]; } else rtn = [temp,temp,temp,temp]; return rtn; }
-
∞ Regex: Date format
<? // Date must match format: mm/dd/yyyy if (!preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $due_date)) echo "Error !";
-
∞ regex - remove 2 or more whitespaces
<? // remove multiple whitespaces $line1 = preg_replace("\s{2,}", " ", $line1);
-
∞ regex to return only first set
<? // extract just the first element $firstset = ereg_replace("^(.*) abc", "\\1", $data); // note: \\1 is the same as javascript's $1
-
∞ regex - replace invalid chars with underscore
<? // replace invalid chars with underscore (good for filename) $name = preg_replace("/[^a-z0-9-]/", "_" ,$name);
-
∞ PHP Regex example of greedy and lazy
<? $x = '<p id="test">p1</p> <p id="test">p2</p>'; // greedy if (preg_match('#<p id="test">(.+)</p>#', $x)) echo "greedy"; // lazy 1 if (preg_match('#<p id="test">(.+?)</p>#', $x)) echo "lazy 1"; // lazy 2 if (preg_match('#<p id="test">([^<]+)</p>#', $x)) echo "lazy 2";
-
∞ common regexes for web applications
<?php // This will contain my regexes that I use. This snipt will be updated when I come across or use more // Username (Must start with an alpha character. Must be between 4 and 21 characters long. Can contain spaces, underscores and hyphens) if (preg_match('/^[a-z]{1,}[a-z0-9 _-]{3,20}$/im', $username)) { echo 'Username is good to go'; } ?>
-
∞ PHP Find & Replace, to fix unquoted array indexes [key] => ['key']
regex: (\[)[a-zA-Z0-9\-]*(\]) replace: ['$2']
-
∞ $4 = column, ~ = pattern, / = start, ^ = beginning of line, $ = end of line, / = end of string
awk '$4 ~/^$/' .htaccess -
∞ Turn link and username text into links
<?php function makeRich($text) { /* * Replacing '@user' and 'http' with hypertext */ $pattern = "/(http:\/\/\S+)/"; // hyperlink pattern if(preg_match($pattern, $text)) { $text = preg_replace($pattern, "<a href='$1'>$1</a>", $text); } // find and link users if(preg_match("/@([^()#@\s\,]+)/", $text)) { $text = preg_replace("/@([^()#@\s\,]+)/", "<a href='http://twitter.com/$1'>@$1</a>", $text); } return $text; } ?>
-
∞ Elimina Linhas em Branco
/* Elimina Linhas em Branco */ :%s/\v^[\n\r]//c
-
∞ Strip Control Characters Out of Var
remove all control characters from $var $var=~ s/[^ -~]//g
-
∞ copy numbered files with regex, no sed
j=9; k=$(( $j+9 )); for f in `find . -name "fooname$j?.txt"`; do echo "copying $f"; cp $f ${f/name$j/name$k}; done;



High Performance MySQL