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

SEARCH KEYWORD -- pattern



  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

  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

  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

  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

  Introducing the for-if anti-pattern

Over the years, I've seen a bunch of coding anti-patterns. I figured maybe I'll share a few. Today, I'll introduce what I'm calling the for-if anti-pattern, also known as "We'll sell you the whole seat, but you'll only need the edge." This is a special case of the for-case anti-pattern, where all but one of the cases is null. for (int i = 0; i < 100; i++) { if (i == 42) { do_something(i); } } This can naturally be simplified to do_something(42); The for-if anti-pattern arises in ma...

   Programming,Anti-pattern,for-if,efficiency     2012-02-02 10:18:15

  The Erlang Design Pattern

Over the last couple of weeks I did an OO programming experiment. I call it the Erlang design pattern. It is based on the Actor model but goes some steps further. At its core just like the Actor model there are active entities (objects) that have a thread and a message queue with the thread waiting on the message queue to do some stuff. The Erlang design pattern extends the Actor model by first dividing the software program into active (actors, that have their own thread) and passive ...

   Erlang,Thread,Pattern,OS Threads     2012-02-06 07:47:56

  One good way to use optional parameter in function

In GoLang, it doesn't support method overloading like in Java, hence sometimes it would be a headache to create functions to construct new structs with different parameters.  Normally, we would construct the new function as below when want to let others create a new struct instance with explicit function call. type Queue struct { Name string } func NewQueue(name string) *Queue { return &Queue{name} } But with the scope and complexity of the struct increases, there might be more prope...

   OPTION PATTERN,VARIADIC FUNCTION,OPTIONAL PARAMETER     2020-09-18 21:45:29

  Singleton Pattern in Golang

Singleton pattern is the simplest design pattern in software design. It ensures that only one instance of an object exists globally, regardless of how many times the object is instantiated. Based on the characteristics of the singleton pattern, it can be applied to scenarios such as global unique configuration, database connection objects, file access objects, etc. In Go language, there are multiple ways to implement the singleton pattern. Today, let's learn together about some of these approach...

   GOLANG,SINGLETON PATTERN,TUTORIAL     2023-08-18 23:52:05

  Singleton Design Pattern in Java

Singleton is frequently used in applications where resource may be expensive to create and no instance specific state needs to be maintained. For example, when creating database connection, a singleton may be needed. Today we will share the famous Singleton design pattern in Java. 1. Definition Singleton design pattern is a design pattern that restricts the instantiation of a class to one object. It is one of the most well-known design patterns. 2. Application Singleton ...

   DESIGN PATTERN,SINGLETON,MULTITHREAD,JAVA     2020-04-11 02:16:28

  Scala, Patterns and The Perl Effect

He tried to understand that one concept for a couple of months before it made sense to him. Admittedly, partial functions are not intuitive for anyone who has been schooled in traditional programming, but still, looking at the problem he was trying to solve it seemed like James was required to expend too much effort relative to the simplicity of the problem (as he pointed out, now that he understands the concept it seems straightforward). He showed me the code, and it was basically a situa...

   Scala,Perl,Pattern,Partial function,Template     2011-12-21 09:25:41