Archive

Archive for April, 2010

Regular Expression Symbols

April 17, 2010 Leave a comment

Introduction:

“Regular expressions” are combination of special characters and symbols used for pattern matching. i.e., you specify a particular combination of such characters and symbols (= regular expression) and the compiler will search for that string of words through the text data. The following is a very short, and hopefully easy-to-follow, introduction to some of the most useful regular expressions.

1. Common matching symbols

Table 1.

Regular Expression Description
. Matches any sign
^regex regex must match at the beginning of the line
regex$ Finds regex must match at the end of the line
[abc] Set definition, can match the letter a or b or c
[abc[vz]] Set definition, can match a or b or c followed by either v or z
[^abc] When a “^” appears as the first character inside [] when it negates the pattern. This can match any character except a or b or c
[a-d1-7] Ranges, letter between a and d and figures from 1 to 7, will not match d1
X|Z Finds X or Z
XZ Finds X directly followed by Z
$ Checks if a line end follows

2. Metacharacters

The following meta characters have a pre-defined meaning and make certain common pattern easier to use, e.g. \d instead of [0…9].

Table 2.

Regular Expression Description
\d Any digit, short for [0-9]
\D A non-digit, short for [^0-9]
\s A whitespace character, short for [ \t\n\x0b\r\f]
\S A non-whitespace character, for short for [^\s]
\w A word character, short for [a-zA-Z_0-9]
\W A non-word character [^\w]
\S+ Several non-whitespace characters

3. Quantifier

A quantifier defines how often an element can occur. The symbols ?, *, + and {} define the quantity of the regular expressions

Table 3.

Regular Expression Description
* Occurs zero or more times
+ Occurs one or more times
? Occurs no or one times, ? is short for {0,1}
{X} Occurs X number of times, {} describes the order of the preceding liberal
{X,Y} .Occurs between X and Y times,
*? ? after a qualifier makes it a “reluctant quantifier”, it tries to find the smallest match.
Categories: 1

Pattern class of Regular Expression in Java

April 17, 2010 2 comments

Introduction:

This article makes you bit more knowledge in java regular expressions. For manage the regular expressions, the java have the three classes in java.util.regex package. But in this article we focused only Pattern class.

A regular expression is a pattern of characters that describes a set of strings. We use the regular expressions to find, display, or modify some or all of the occurrences of a pattern in an input sequence.

Java.util.regex classes:

The package java.util.regex contains three classes such as,

  • Pattern Class
  • Matcher Class
  • PatternSyntaxExcpetion

Let us take a look at Pattern class.

Pattern Class:

A regular expression which is specified as a string that should be first compiled into an instance of Pattern class. The resulting pattern can be used to create an instance of  Matcher class which contains various in-built methods that helps in performing a match against the regular expression. Many Matcher objects can share the same Pattern object.

Create Pattern using compile():

Pattern class doesn’t have a public constructor. So by using the static method compile we can create the pattern.

Pattern p = Pattern.compile("my regexp");

For Regular expression symbols,click here

Important Note:

The backslash is an escape character in Java Strings. i.e., backslash has a predefine meaning in Java. You have to use “\\” instead of “\”.

If you want to define “\w” then you must be using “\\w” in your regex like this.

Pattern r = Pattern.compile(“\\w+”); //Place your pattern here

“\w“ represents a word character, i.e., short for [a-zA-Z_0-9]

We can create the Pattern with flags.

Syntax:

Pattern pattern=Pattern.compile(regex,flags);

For ex, If we want to neglect the case sensitive we can achieve by using below one,

Pattern pattern = Pattern.compile(“\\w+”,Pattern.CASE_INSENSITIVE);

Validate pattern using matches():

The matches() method is used to check whether the given input is match with the pattern. This method returns true only if the entire input text matches the pattern.

boolean isMatch = Pattern.matches(“\\w+”,”Welcome to java world”);

Get the Pattern using pattern():

The pattern() method is used for find out the pattern of the given string. This method returns the regular expression as a string from which this pattern was compiled.

Pattern p=input.pattern();

Split input using split():

The split() method is used to split the given input text based on the given pattern. It returns a String array. There are two forms of split() method,

  • split(String input)
  • split(String input, int limit)

In the second form, we have an argument called limit which is used to specify the limit i.e. the number of resultant strings that have to be obtained by split() method.

String[] str = pattern.split(input,3);

Sample code for Pattern class and methods:

MailID Validation.java:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Home
 */
class MailIDValidation
{
 public static void main(String args[])
 {
 //Input the string for validation
 String email = "mymail@gmail.com";

 //Set the email pattern string
 Pattern p = Pattern.compile(".+@.+\\.[a-z]+");

 //Match the given string with the pattern
 Matcher m = p.matcher(email);

 //check whether match is found
 boolean matchFound = m.matches();

 if (matchFound)
 System.out.println("Valid Email Id.");
 else
 System.out.println("Invalid Email Id.");
 }
}

I hope it offer bit more knowledge in Regular Expressions. The next article makes you clear in Matcher class in regular expression.Please leave your footprints (comments) here.

10 Fixes that solve IE Issues

April 3, 2010 6 comments

Introduction:

Whenever i designed my web page i have found out some issues that issues make my design is  not compatible with a wide range of browsers.So in this article i add some tips for making your website compatible with a wide range of browsers .

1. Solve invisibility problems:

If you face any problems like the image,text component are invisible sometimes, you add the property position:relative for the components to avoid the invisibility problems.

2. Use display: inline for floated elements

Floated elements with a margin can fire the famous IE6 double-margin bug, e.g. you specify a left margin of 5px and actually get 10px. Display:inline will fix the problem and, although it should not be required, your CSS remains valid.

3.  Alternative for colon in components ID:

The IE does not support colon for represents the components ID.If you want to refer the components ID which has colon you

replace the colon with the value \003A

For ex,

<h1 id="formID:titleID">Welcome to My world</h1>

Css for Mozilla:

h1#formID:titleID
{
//some properties
}

For IE:

h1#formID\0003AtitleID
{
//Some properties
}

4. Internet Explorer hacks

While conditional comments are better, you can also target some versions of Internet Explorer using the following syntax:

body
{
Width: 200px; /* All browsers */
*width: 250px; /* IE */
_width:300px; /* IE6 */
.width:200px; /* IE7 */
}

This technique is not W3C compliant (this is why you should use conditional comments instead) but sometimes, it is a real time saver.

5. Avoid percentage dimensions

Percentages confuse IE. Unless you can carefully size every parent element, they are probably best avoided. You can still use percentage values in other browsers with !important, e.g.

Body
{
height: 2%; !important
height: 20px; /*IE6 only*/
}

6. Another box model hack alternative

Box model hack is used to fix a rendering problem in pre-IE 6 browsers on PC, where by the border and padding are included in the width of an element, as opposed to added on. Number of CSS-based solutions has been put forward to remedy this, so here’s another one which we really like:

padding: 2em;
border: 1em solid green;
width: 20em;
width/**/:/**/ 14em;

The first width command is read by all browsers; the second by all browsers except IE5.x on PC. Because the second command comes second it takes precedence over the first – any command that comes second will always override a preceding command. So, how does all this work?

By placing empty comment tags (/**/) before the colons, IE5.0 will ignore the command. Likewise, by placing these empty comment tags after the colon, IE5.5 will ignore the command. By using these two rules in conjunction with each other, we can hide the command from all of IE5.x.

7.  Class

The class selector allows you to set multiple styles to the same element or tag in a document. For example, you might want to have certain sections of your text called out in different colors from the rest of the text in the document. You would assign your paragraphs with classes like this:

P.blue
{
background-color: #0000ff;
}
P.red
{
background-color: #ff0000;
}

Then, when you want to call the various classes you would use the CLASS attribute within the paragraph tag. Any tag that did not have a class attribute would be given the default style for that tag. For example:

<p>Welcome in blue background</p>
<p>Hello world in red background</p>

8. ID

The ID selector allows you to give a name to a specific style without associating it with a tag or other HTML element. You write an ID code like this

h3#indent1 {text-indent: 10px ;}

You associate an ID tag the same way you associate classes, within the element that should have that style:

<h3 id="indent1">This test for ID CSS</h3>

You can give your ID tags any grouping of letters and numbers that you would like.Keep in mind that HTML standards require that the ID must be unique.

Note:

One of the trickiest things about CSS is that it requires the use of formerly optional ending tags (as does XML). For example, if you have a style applied to a paragraph tag <p>, the browser may not know where to end that style if you do not include the ending tag </p>.

9.  Class and ID together

If we need to assign the same style class or id we can assign different style for each one with using classes and ID together. For ex,

<h1 id="blueheaderId" class="blue">Welcome to CSS</h1>
h1#blueheaderId.blue {color: blue ;}

10. Refractor your code

Often, it can take longer to fix than re-think a layout problem. Minor alterations to the HTML and simpler CSS are often more effective. This may mean you abandon perfectly legitimate code, but fewer long-term issues will arise and you know how to handle the problem in future.

Have I missed your favorites IE fix? Comments and suggestions welcome.