Archive

Archive for the ‘Uncategorized’ Category

Javascript Regular Expression greedy vs lazy

February 4th, 2012

We will understand the concept of greedy Vs. Lazy regular expression with the help of
an example.

Consider part of an html page consisting of some words in bold. Here is an example

<p> This is an example page </p>
<b> First Bold </b>
Something here
<b> Second Bold </b>
Something else here
<p> Finish </p>
Now, we want to write a regular expression which will match <b> First Bold
</b> in the above example. To do this we use the following regular expression
/<b>.*</b>/
Hoping it to work. This regular expression, however, matches the following part of the
subject
<b> First Bold </b>
Something here
<b> Second Bold </b>
Why ? To understand it we would like to understand the mechanism of match. In the
regular expression /<b>.*</b>/ . The regular expression first looks for <b> in the
subject. Once <b> is found, .* all way to the end of the subject. In the process, it eats
up the whole subject, right all way to the finish </p>. When its stomach is full, it looks
in the regular expression, for what next to match. It is </b>. So what it does is
backtracking.

 

It tries to match < of </b> in the regular expression with > of </p> in the subject. The
match fails. Now it tries matching < of </b> with p of </p>. This also fails. It keeps
doing this till it matches < of </b> in the regular expression with < of </p> in the
subject

<html>
<body>
<script type=”text/javascript”>
<!–
/*
********************************************************
Javascript Regular Expression Example ch4 Ex 01
Understanding Greedy Vs. Lazy Match
********************************************************
*/
var pattern1=/<b>(.*)<\/b>/;
var string1 = “<p> This is an example page </p><b> First Bold </b>Something here
<b> Second Bold </b> Something else here<p> Finish </p>” ;
var string2 = string1.match(pattern1);
document.write(“string2[0] is : “, string2[0] , “<br />”);
//–>
</script>
</body>
</html>

 

If we run this code we get the following output

string2[0] is : First Bold Something here Second Bold

Notice that, since we are displaying the output on an html, we do not see <b> and </b>.
We instead see the actual bold letters. But you should be able to feel the idea. The
regular expression /<b>(.*)<\/b>/; matches all the text between the first <b> and all
way to the last </b>.

If we change the statement

var pattern1=/<b>(.*)<\/b>/;
to
var pattern1=/<b>(.*?)<\/b>/;

we get the following output

string2[0] is : First Bold

You may also like to take a small quiz about Regex ( greedy vs lazy)

Also check

1. Javascript Tutorial for beginners

2. Regular Expression In Javascript on amazon

 

 

 

Uncategorized

Javascript Textarea Tutorial

February 4th, 2012

Javascript texarea is useful construct that you can use to input a large area of text for further processing and output. A typical example could be entering simple text for further processing. In today’s tutorial let us learn about Javascript Textarea with an example.

In the example we will present user with a text area and a button. We need to accomplish some task, based upon clicking the button. Let us get started. Copy the following text and start seeing its effects.







If you are a beginner – you may like to take a look at the Javascript Tutorial.

Uncategorized

How to find the Number of Cores and Logical Processors using C#.net

January 30th, 2012

This is a pre cusrsor to something more that we are trying to achieve. The goal is to develop a simple code that will find the processor and memory details and then will benchmark the computer. We will then let users run this program to help him benchmark and compare the processors.

Today we will show you how to get the information about the number of processors. Here is the code that has been compiled in C#.Net ( 2010 Express Edition).

A note on compilation.

- Copy paste this code in console application and then go to Projects -> Add reference – > .NET – > System.Management

Happy coding.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get())
{
Console.WriteLine("Number Of Physical Processors: {0} ", item["NumberOfProcessors"]);
}

// Number of Cores

int coreCount = 0;
foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_Processor").Get())
{
coreCount += int.Parse(item["NumberOfCores"].ToString());
}
Console.WriteLine("Number Of Cores: {0}", coreCount);

// Number of Logical Processors

foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get())
{
Console.WriteLine("Number Of Logical Processors: {0}", item["NumberOfLogicalProcessors"]);
}

Console.ReadLine();
}
}
}

Uncategorized

Allegro PCB Viewer

January 29th, 2012

If you have been given an Allegro PCB Design to review and you do not have the access to complete Allegro Design Suites, you may like to download the Allegro PCB Viewer from http://www.cadence.com/products/pcb/Pages/downloads.aspx. The Allegro PCB viewer is a free tool to view the Allegro PCB Design files.

If you have the complete tool available to you, you may like to check the Allegro PCB tutorial.

Uncategorized

New Calculators – Post errors in Comment

January 21st, 2012

AMD Athlon ii P340 Processor review

November 1st, 2010

[ad#2]
AMD has launched in the market an Athlon II dual-core processor P340. The features of this processor make it a great competition for the recently released Intel Pentium P6100 series. Let’s have a look at the characteristics that make the Athlon II P340 a good option for customers in their search for a good processor for their laptops and/or notepads. The Athlon II P340 has a frequency of operation of 2.2 GHz. Another feature we should mention is that it has a CPU multiplier is of 16. When compared to the Intel Pentium p6100 series, the Athlon II P340 processor offers a memory controller of the DDR3, this means that the there is a reduction of the memory interval and it also means that there is an improvement in the processor as a whole. The dual-core has been built with 45nm lithography technology, and has a data width of 64 bits. It also offers a level 2 cache memory separated in the dual core and with 512 KB each level. Another striking characteristic of the Athlon II P340 processor is that the power consumption is of only 25 watts, the lowest power consumption processor when compared to other CPUs in the same market segment.
[ad]
The Athlon ii supports Hardware virtualization. The advantage of such feature is that you will be able to have different operating systems in the same device without having to cope with a decrease in performance. One thing you should take into account when considering to purchase a laptop with this type of processor is that it has a revolutionary technology to avoid heating problems (Powernow and Cool’n’Quit). Some other things that the Athlon II P340 processor includes are an adjustable controller, hardware to help you control temperature of the processor, and some integration based samples. The new dual-core processor from AMD also includes an outstanding feature to help you protect the timer support. The reason why I believe this is a good processor to get is because all the available graphic cards in the market can be used with the Athlon II P340. Nevertheless, as the speed for processing data is not much it does not offer an excellent performance when the customer uses applications that require multi threading. You will find this type of Athlon II P340 processor in brands such as Sony Vaio, HP and Acer, but not all models come with a dual-core processor P340.

Uncategorized