<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: regexp code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 07:14:56 GMT</pubDate>
    <description>DZone Snippets: regexp code</description>
    <item>
      <title>Javascript count line breaks</title>
      <link>http://snippets.dzone.com/posts/show/5770</link>
      <description>Returns the number of line breaks in a string.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function lineBreakCount(str){&lt;br /&gt;	/* counts \n */&lt;br /&gt;	try {&lt;br /&gt;		return((str.match(/[^\n]*\n[^\n]*/gi).length));&lt;br /&gt;	} catch(e) {&lt;br /&gt;		return 0;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 15 Jul 2008 08:57:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5770</guid>
      <author>featurelover (Jochen Preusche)</author>
    </item>
    <item>
      <title>Rewrite input to friendly URL</title>
      <link>http://snippets.dzone.com/posts/show/5629</link>
      <description>// Take an input and rewrite it to a friendly-url-like-this&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;&lt;br /&gt;&lt;br /&gt;	// Store the current title value&lt;br /&gt;	var title = 'This is a title with a symbol &amp;'&lt;br /&gt;&lt;br /&gt;	// alert(title); // debug&lt;br /&gt;&lt;br /&gt;	// Clean up the title		&lt;br /&gt;	var url = title&lt;br /&gt;		.toLowerCase() // change everything to lowercase&lt;br /&gt;		.replace(/^\s+|\s+$/g, "") // trim leading and trailing spaces		&lt;br /&gt;		.replace(/[_|\s]+/g, "-") // change all spaces and underscores to a hyphen&lt;br /&gt;		.replace(/[^a-z0-9-]+/g, "") // remove all non-alphanumeric characters except the hyphen&lt;br /&gt;		.replace(/[-]+/g, "-") // replace multiple instances of the hyphen with a single instance&lt;br /&gt;		.replace(/^-+|-+$/g, "") // trim leading and trailing hyphens				&lt;br /&gt;		; &lt;br /&gt;	&lt;br /&gt;	alert(url); // outputs 'this-is-a-title-with-a-symbol'&lt;br /&gt;&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Jun 2008 20:30:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5629</guid>
      <author>milkshake (milkshake)</author>
    </item>
    <item>
      <title>Extract the filename from a URL</title>
      <link>http://snippets.dzone.com/posts/show/5407</link>
      <description>This ECMAScript extracts the filename from a URL. eg. the filename 'index4.svg' would be extracted from the document.URL with the value 'http://rorbuilder.info/r/whiteboardqueue/index4.svg'.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;var regexp = /(\w|[-.])+$/&lt;br /&gt;str = document.URL&lt;br /&gt;a = regexp.exec(str)&lt;br /&gt;alert(a[0])&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Reference: &lt;a href="http://www.webreference.com/js/column5/methods.html"&gt;Regular Expressions: Methods - Doc JavaScript&lt;/a&gt; [webreference.com]</description>
      <pubDate>Sun, 20 Apr 2008 15:24:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5407</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Some perl one-liners</title>
      <link>http://snippets.dzone.com/posts/show/4127</link>
      <description>Rename htm files to html&lt;br /&gt;In windows: &lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;dir /B /S | perl -wlne"/([^ ]+)\.htm$/i&amp;&amp;rename$1.'.htm',$1.'.html'"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;In linux: &lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;find | grep htm | perl -wlne'/([^ ]+)\.htm$/i&amp;&amp;rename$1.".htm",$1.".html"'&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Sun, 10 Jun 2007 18:00:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4127</guid>
      <author>griflet (guillaume riflet)</author>
    </item>
    <item>
      <title>Check for primality with a regexp</title>
      <link>http://snippets.dzone.com/posts/show/3691</link>
      <description>Original perl code here:&lt;br /&gt;&lt;br /&gt;http://montreal.pm.org/tech/neil_kandalgaonkar.shtml&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Fixnum&lt;br /&gt;  def prime?&lt;br /&gt;    ('1' * self) !~ /^1?$|^(11+?)\1+$/&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 18 Mar 2007 14:48:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3691</guid>
      <author>ciconia (Sharon Rosner)</author>
    </item>
    <item>
      <title>Create an regexp (Regular expression) from an array of Strings</title>
      <link>http://snippets.dzone.com/posts/show/3167</link>
      <description>// description of your code here&lt;br /&gt;Create a DNF regular expression from an array of strings even if they have special characters. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Regexp.new(array.collect{|string| Regexp.escape(string)}.join("|"))&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 19 Dec 2006 12:36:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3167</guid>
      <author>eddiewould (Eddie)</author>
    </item>
    <item>
      <title>Simple regexp in python working on a string of names and e-mail adresses</title>
      <link>http://snippets.dzone.com/posts/show/3065</link>
      <description>Simple regexp in python working on a string of names and e-mail adresses&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env python&lt;br /&gt;&lt;br /&gt;import re&lt;br /&gt;import sys&lt;br /&gt;&lt;br /&gt;mySplit = re.compile (r',', flags=re.MULTILINE)&lt;br /&gt;mailadress = re.compile (r'\&lt;.\D*.\D+@\D+.[.]\D+.\&gt;', flags=re.MULTILINE)&lt;br /&gt;&lt;br /&gt;for i in mySplit.split ("Weng Feng &lt;weng.feng@telia.se&gt;, Erik Andersson &lt;kirean@gmail.com&gt;, Ake Kong &lt;kong@\&lt;br /&gt;goek.se&gt;"):&lt;br /&gt;    k = mailadress.search(i.strip())&lt;br /&gt;    if k:&lt;br /&gt;        print k.group().lstrip("&lt;").rstrip("&gt;")&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 01 Dec 2006 13:51:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3065</guid>
      <author>kirean73 (Erik Andersson)</author>
    </item>
    <item>
      <title>Split String into roughly equal-sized chunks.</title>
      <link>http://snippets.dzone.com/posts/show/2631</link>
      <description>Split a string into an array of roughly equal sized chunks based on a string or regular expression delimiter.&lt;br /&gt;Delimiter is preserved in output.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class String&lt;br /&gt;  def chunk_string(average_segment_size = 40, sclice_on = /\s+/)&lt;br /&gt;    out = []&lt;br /&gt;    slices_estimate = self.size.divmod(average_segment_size)&lt;br /&gt;    slice_count = (slices_estimate[1] &gt; 0 ? slices_estimate[0] + 1 : slices_estimate[0])&lt;br /&gt;    slice_guess = self.size / slice_count&lt;br /&gt;    previous_slice_location = 0&lt;br /&gt;    (1..slice_count - 1).each do&lt;br /&gt;      |i|&lt;br /&gt;      slice_location = self.nearest_split(slice_guess * i, sclice_on)&lt;br /&gt;      out &lt;&lt; self.slice(previous_slice_location..slice_location)&lt;br /&gt;      previous_slice_location = slice_location + 1&lt;br /&gt;    end&lt;br /&gt;    out &lt;&lt; self.slice(previous_slice_location..self.size)&lt;br /&gt;    out&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def nearest_split(slice_start, slice_on)&lt;br /&gt;    left_scan_location  = (self.slice(0..slice_start).rindex(slice_on)).to_i&lt;br /&gt;    right_scan_location = (self.slice((slice_start+1)..self.size).index(slice_on)).to_i + slice_start&lt;br /&gt;    ((slice_start - left_scan_location) &lt; (right_scan_location - slice_start) ? left_scan_location : right_scan_location)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 21 Sep 2006 00:29:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2631</guid>
      <author>duncanbeevers (Duncan Beevers)</author>
    </item>
    <item>
      <title>Return the extension from a file name</title>
      <link>http://snippets.dzone.com/posts/show/2555</link>
      <description>// Return the extension from a file name&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/(.*\.)(.*$)/.match(File.basename(file_name))[2]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 07 Sep 2006 02:06:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2555</guid>
      <author>duncanbeevers (Duncan Beevers)</author>
    </item>
    <item>
      <title>Make anchors from urls and email addresses</title>
      <link>http://snippets.dzone.com/posts/show/617</link>
      <description>This little PHP function will find urls and email addresses in a block of text and turn them into hyperlinks and mailto: anchors respectively.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function makeLinks($sourceText) {&lt;br /&gt;  $destText = preg_replace( "/([_a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+)(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,4})/", '&lt;a href="mailto:\\0"&gt;\\0&lt;/a&gt;',$sourceText);&lt;br /&gt;  $destText = preg_replace_callback('/\bhttp[^\s]+/',create_function('$matches', 'return "&lt;a href=\"$matches[0]\"&gt;" . preg_replace("#(\.|/)#", "&amp;shy;$1", $matches[0]) . "&lt;/a&gt;";'),$destText);&lt;br /&gt;  return $destText;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 01 Sep 2005 02:44:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/617</guid>
      <author>hjalli (Hjalmar Gislason)</author>
    </item>
  </channel>
</rss>
