Today's Question:  What weekend projects have you created?

#### A Perl Regular Expression That Matches Prime Numbers

perl -lne '(1x\$_) =~ /^1?\$|^(11+?)\1+\$/ || print "\$_ is prime"' Can you figure out how it works? I give an explanation below, but try to figure it out yourself. Here is what happens when you run it: \$ perl -lne '(1x\$_) =~ /^1?\$|^(11+?)\1+\$/ || print "\$_ is prime"' 1 2 2 is prime 3 3 is prime 4 5 5 is prime 6 7 7 is prime 8 9 10 11 11 is prime Here is how it works. First, the number is converted in its unary representation by (1x\$_). For example, the number 5 gets converted into 1x5, which is ...

Perl,Regex,Regular expression,Prime number,One line     2011-12-26 08:42:00

#### Regular expression to get html meta description

When we need to process a HTML page source code, we often need to retrieve the meta description of the page besides the links in the page. This description is usually located in <meta> tag of a HTML page. The meta description is very useful for search engine index. How can we retrieve the meta description? If we use a regular expression, we can easily get the meta description. In JavaScript, the regular expression looks like : var pattern = /<meta.*?name="description".*?content="(.*?)"....

Regular expression,meta description,HTML,JavaScript     2012-07-03 10:09:20

#### Get hostname from a URL using JavaScript

Sometimes we may have strings which contain some UR;s and we may want to retrieve the hostname from the URLs for some statistic use. For example, we may have a URL : http://www.example.com/aboutus.html. We may want to retrieve the www.example.com from the URL. How? Use regular expression. Here I give an example using JavaScript. If you want to check whether a string is a URL or not. Refer to Detect URLs in a Block of Text. In JavaScript, we can have a regular expression like var pattern=/(.+:\/\...

JavaScript,URL,regular expression, Hostname     2012-06-15 09:16:45

#### Regex vs IndexOf in Java.

Overview The author of these articles Are C# .Net Regular Expressions Fast Enough for You? and .Net Regex: Can Regular Expression Parsing be Faster than XmlDocument or Linq to Xml? recently pointed out to me that he had found that Regular expressions were at least as fast or faster than the alternatives in C#. However it has been my experience that regular expressions in Java were slower. It is hard for me to say why this might be different in Java and C# or even if these are fair compari...

Java,indexOf,Regular Expression,Efficiency     2012-01-03 10:58:52

#### The Greatest Regex Trick Ever (Simplified)

There is a post which is really hot recently which showcased a best ever regular expression trick. In this post, it provides a trick which can be adapted to situations where one wants to match some patterns desired and exclude the patterns undesired. In simple, the trick is : Pattern not desired | (Pattern desired) Then taking the group 1 match of the capturing group of matched. This group will contain the strings matching the desired patterns. Have to say this trick is really neat and brilliant...

REGULAR EXPRESSION,PROGRAMMING,JAVA     2015-10-01 21:59:05

#### Getting started with C++ TR1 regular expressions

Overview This article is written for the benefit of someone familiar with regular expressions but not with the use of regular expressions in C++ via the TR1 (C++ Standards Committee Technical Report 1) extensions. Comparisons will be made with Perl for those familiar with Perl, though no knowledge of Perl is required. The focus is not on the syntax of regular expressions per se but rather how to use regular expressions to search for patterns and make replacements. Support for TR1 ext...

Regular expression,Replace,TR1,Extension     2011-08-14 07:25:20

#### How regular expression works

Rob Pike wrote 30 lines of codes to realize a simple regular expression matcher in his book The practice of Programming. This piece of code is really cool. Let's take a look at the code.Meaning of different characters.Character Meaning c General character . Match any single character ^ Match start of a string \$ Match end of a string * Match zero or many occurrences of a character /*match ：Test the regexp in text*/int match(char* regexp,char* text){    if(regexp[0] == '^') ...

Regular expression,Implementation,Rob Pike,C     2012-06-25 05:23:41

#### this keyword in Lambda expression in Java 8

Since the release of Java 8, people are excited to see a big feature added to this language which is existing in other languages for a long time -- Lambda expression. The introduction of lambda expression in Java 8 gives people an easy way or a vertical to horizontal way to solve problems. There are many posts on the Internet which shows how to use lambda expression in Java, such as Lambda Quick Start and Java 8 Lambda Expressions Tutorial with Examples. In this post, we will only focus on the t...

this,lambda expression,Java 8     2014-06-01 03:57:05

#### Greedy and Nongreedy Matching in a Regular Expression

By default, pattern matching is greedy, which means that the matcher returns the longest match possible. For example, applying the pattern A.*c to AbcAbcA matches AbcAbc rather than the shorter Abc. To do nongreedy matching, a question mark must be added to the quantifier. For example, the pattern A.*?c will find the shortest match possible. COPY // Greedy quantifiers String match = find("A.*c", "AbcAbc"); // AbcAbc match = find("A.+", "AbcAbc"); // AbcAbc // Nongreedy quantifier...

Regular expression,Pattern match,Greedy,     2011-08-09 12:42:28

#### Read white space with scanf()

Usually, when we want to enter string with white spaces in C, we need to call gets() or fgets(0 method. We usually will not use scanf(0 or fscanf() because they cannot accept white spaces when scan user inputs.   But when we specify the format in scanf() function, we may read strings with white space. the code section below illustrate this: #include <stdio.h>   int main(int argc,char **argv){        char name[30];      fprintf(stdout,"Please en...

C,scanf,white space, string format     2011-09-26 11:45:43