Sign up to create your own snipts, or login.

Public snipts » regex The latest public regex snipts.

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 );
    }
    

    copy | embed

    0 comments - tagged in  posted by robertbanh on Jan 22, 2010 at 3:28 p.m. EST
  • Regex for static tag replacement.
    re.sub("<p>\s*?&nbsp;\s*?</p>", "REPLACEMENT", string)
    

    copy | embed

    0 comments - tagged in  posted by gotoplanb on Jan 14, 2010 at 6:05 p.m. EST
  • 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);
    	}
        }
    }
    

    copy | embed

    0 comments - tagged in  posted by lenni on Dec 28, 2009 at 4:23 a.m. EST
  • Replace any whitespace with only a single space
    function replace_whitespace($Value = '')
    {
    	return preg_replace('/\s+/', ' ', trim($Value));
    }
    

    copy | embed

    0 comments - tagged in  posted by RiSiCO on Dec 05, 2009 at 6:38 p.m. EST
  • 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 
    

    copy | embed

    0 comments - tagged in  posted by rschu68 on Dec 02, 2009 at 9:51 a.m. EST
  • URL Regex
    \b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))
    

    copy | embed

    0 comments - tagged in  posted by rushi on Dec 01, 2009 at 7:33 a.m. EST
  • multiple regex replacement from a single command
    sed -e 's/root/r00t/;s/regular/not/' testing
    

    copy | embed

    0 comments - tagged in  posted by malkir on Nov 17, 2009 at 3:39 a.m. EST
  • 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;
    }
    

    copy | embed

    0 comments - tagged in  posted by catcubed on Nov 16, 2009 at 11:37 a.m. EST
  • Regex: Date format
    <?
    // Date must match format: mm/dd/yyyy
    if (!preg_match('/^\d{2}\/\d{2}\/\d{4}$/', $due_date))
      echo "Error !";
    

    copy | embed

    0 comments - tagged in  posted by robertbanh on Sep 15, 2009 at 3:38 p.m. EDT
  • regex - remove 2 or more whitespaces
    <?
    // remove multiple whitespaces
    $line1 = preg_replace("\s{2,}", " ", $line1);
    

    copy | embed

    0 comments - tagged in  posted by robertbanh on Sep 10, 2009 at 8:08 p.m. EDT
  • 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
    

    copy | embed

    0 comments - tagged in  posted by robertbanh on Sep 10, 2009 at 8:08 p.m. EDT
  • regex - replace invalid chars with underscore
    <?
    // replace invalid chars with underscore (good for filename)
    $name = preg_replace("/[^a-z0-9-]/", "_" ,$name);
    

    copy | embed

    0 comments - tagged in  posted by robertbanh on Sep 10, 2009 at 8:05 p.m. EDT
  • 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";
    

    copy | embed

    0 comments - tagged in  posted by robertbanh on Sep 10, 2009 at 7:51 p.m. EDT
  • 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';
    }
    
    ?>
    

    copy | embed

    0 comments - tagged in  posted by Rhinos on Aug 10, 2009 at 9:05 p.m. EDT
  • PHP Find & Replace, to fix unquoted array indexes [key] => ['key']
    regex:
    (\[)[a-zA-Z0-9\-]*(\])
    
    replace:
    ['$2']
    

    copy | embed

    0 comments - tagged in  posted by pkarl on Jul 20, 2009 at 9:36 a.m. EDT
  • $4 = column, ~ = pattern, / = start, ^ = beginning of line, $ = end of line, / = end of string
    awk '$4 ~/^$/' .htaccess 
    

    copy | embed

    0 comments - tagged in  posted by malkir on Jul 14, 2009 at 4:59 p.m. EDT
  • 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;
    }
    
    ?>
    

    copy | embed

    0 comments - tagged in  posted by johnmangino on Jul 01, 2009 at 10:57 p.m. EDT
  • Elimina Linhas em Branco
    /* Elimina Linhas em Branco */
    :%s/\v^[\n\r]//c
    

    copy | embed

    0 comments - tagged in  posted by assolan on Jun 25, 2009 at 1:32 p.m. EDT
  • Strip Control Characters Out of Var
    remove all control characters from $var
    
    $var=~ s/[^ -~]//g
    

    copy | embed

    0 comments - tagged in  posted by resmith100 on Apr 04, 2009 at 6:45 p.m. EDT
  • 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;
    

    copy | embed

    0 comments - tagged in  posted by hallmark on Mar 16, 2009 at 10:13 p.m. EDT
Sign up to create your own snipts, or login.