IMPORTANT!

Snipt is going open source. We've toyed with this idea for quite a while, and have finally decided it's the right way to move forward.

A few things:
  • The entire Snipt source code will be released on GitHub under the 3-clause BSD License on Friday, September 10th.
  • While we'd like to think we're perfect, we realize we're only human. By open sourcing the software that runs this website, certain bugs or security flaws may be discovered that could compromise the privacy of your snipts.
  • Only the Lion Burger team will be able to push commits to the Snipt.net site. Contributors should send a pull request to add new features or submit patches.
  • By using this site, you agree not to be too angry or take any legal action against Lion Burger should this whole thing go up in flames some day.
  • Follow us on Twitter for updates.
I agree, close this message
Sign up to create your own snipts, or login.

Latest 100 public snipts » regex The latest public regex snipts.

showing 1-20 of 49 snipts for regex
  • Regex remove img tag
    $content = preg_replace("/<img[^>]+\>/i", "", $content);
    

    copy | embed

    0 comments - tagged in  posted by jameswmcnab on Aug 28, 2010 at 5:14 p.m. EDT
  • Perl remove html tags
    #!/usr/bin/perl 
    while (<STDIN>) {
    s/<[a-zA-Z\/][^>]*>//g;
    print $_;
    }
    

    copy | embed

    0 comments - tagged in  posted by bradrice on Jul 21, 2010 at 3:21 p.m. EDT
  • 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;
    

    copy | embed

    0 comments - tagged in  posted by fevangelou on Jun 28, 2010 at 3:32 p.m. EDT
  • 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);
    
    });
    

    copy | embed

    0 comments - tagged in  posted by mountainash on Jun 27, 2010 at 8:38 p.m. EDT
  • 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
    

    copy | embed

    0 comments - tagged in  posted by joeybaker on Jun 04, 2010 at 3:42 p.m. EDT
  • 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.";
    }
    ?>
    

    copy | embed

    0 comments - tagged in  posted by richard on Jun 02, 2010 at 6:52 a.m. EDT
  • 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
    

    copy | embed

    0 comments - tagged in  posted by zackgilbert on May 28, 2010 at 4:41 p.m. EDT
  • 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'\)
    

    copy | embed

    0 comments - tagged in  posted by jpartogi on Apr 21, 2010 at 2:44 a.m. EDT
  • 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);
    

    copy | embed

    0 comments - tagged in  posted by corbanb on Apr 01, 2010 at 3:58 p.m. EDT
  • Is GIF, JPG, PNG
    var pattern:RegExp = ([^\s]+(?=\.(jpg|gif|png))\.\2);
    

    copy | embed

    0 comments - tagged in  posted by corbanb on Apr 01, 2010 at 3:56 p.m. EDT
  • 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!
    

    copy | embed

    0 comments - tagged in  posted by corbanb on Apr 01, 2010 at 3:39 p.m. EDT
  • Some useful regex...
    // Validate URL
    /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \?=.-]*)*\/?$/
    
    // Unclosed image tags
    <img([^>]+)(\s*[^\/])>
    

    copy | embed

    0 comments - tagged in  posted by xupisco on Mar 31, 2010 at 5:20 p.m. EDT
  • 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);
    }
    

    copy | embed

    0 comments - tagged in  posted by vagrantradio on Mar 31, 2010 at 9:36 a.m. EDT
  • 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]*([^\?>]*)\?>
    

    copy | embed

    0 comments - tagged in  posted by vagrantradio on Mar 31, 2010 at 9:13 a.m. EDT
  • 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
Sign up to create your own snipts, or login.