Today's Question:  What does your personal desk look like?        GIVE A SHOUT

 ALL


  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. Here is a simple explanation on how this works. The above regular expression will first try to m...

4,702 0       JAVA PROGRAMMING REGULAR EXPRESSION


  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="(.*?)".*?>|<meta.*?content="(.*?)".*?name="description".*?>/i;since the description is the content i...

16,061 0       JAVASCRIPT HTML REGULAR EXPRESSION META DESCRIPTION


  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.CharacterMeaningcGeneral 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] == '^')        return matchhere(regexp+1,text);     do{ /*Check the strin...

3,193 0       C REGULAR EXPRESSION IMPLEMENTATION ROB PIKE


  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=/(.+:\/\/)?([^\/]+)(\/.*)*/i;the regular expression pattern can be used to get the hostname. There are three...

18,456 0       JAVASCRIPT REGULAR EXPRESSION URL HOSTNAME


  Regex vs IndexOf in Java.

OverviewThe 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 comparisons but here is what I found in Java.Searching XML for the start of a fieldThis test searches for the exp...

6,097 0       JAVA REGULAR EXPRESSION EFFICIENCY INDEXOF


  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"'122 is prime33 is prime455 is prime677 is prime89101111 is primeHere 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 11111 (1 repeated 5 times.)Next, the unary string gets tested against the regular expression. If it matches, the number is ...

5,310 0       REGULAR EXPRESSION PERL REGEX PRIME NUMBER ONE LINE


  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 extensions in Visual Studio 2008 is added as a feature pack. It is also included in Visual Studio 2010. Other im...

4,058 0       EXTENSION REGULAR EXPRESSION REPLACE TR1


  Greedy and Nongreedy Matching in a Regular Expression

By default, pattern matching is greedy, which means that the matcherreturns the longest match possible. For example, applying the patternA.*c to AbcAbcA matches AbcAbc rather than the shorterAbc. To do nongreedy matching, a question mark must be added tothe quantifier. For example, the pattern A.*?c will find theshortest match possible.COPY// Greedy quantifiersString match = find("A.*c", "AbcAbc"); // AbcAbcmatch = find("A.+", "AbcAbc"); // AbcAbc// Nongreedy quantifiersmatch = find("A.*?c", "AbcAbc"); // Abcmatch = find("A.+?", "AbcAbc"); // Abc// Returns the first s...

3,656 0       REGULAR EXPRESSION PATTERN MATCH GREEDY