Friday, December 19, 2008

regular Expression Samples

This is good example for regular Expression

http://www.phpf1.com/tutorial/php-regular-expression.html

Useful php link for regular expression extractor

HTML FILE READ using regular expresssion

Simple PHP Examples

Regular expression Cheat Sheet



The regular expressions basic syntax
To use regular expressions first you need to learn the syntax of the patterns. We can group the characters inside a pattern like this:
Normal characters which match themselves like hello
Start and end indicators as ^ and $
Count indicators like +,*,?
Logical operator like
Grouping with {},(),[]
An example pattern to check valid emails looks like this:
Code: ^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$

Count indicators like +,*,?

“+” : There is at least 1 "d" after "worl"
“*” : There is 0 or more "d" after "worl"
“?” : There is 0 or 1 "d" after "worl"

Logical operator like

“” : The string contains the "earth" or the "world"

Grouping with {},(),[]

{} : deals with last single character
world{1} There is 1 "d" after "worl"
world{1,} There is 1 ore more "d" after "worl"
world{2,3} There are 2 or 3 "d" after "worl"
()
wo(rld)* There are 2 or 3 "d" after "worl"
[]
[abc] There is an "a" or "b" or "c" in the string abc, bbaccc

Other Symbols “.”, ”-“, “^”

. Any character in place of the dot.

- [a-z] There are any lowercase letter in the string
[a-zA-Z] There are any lower- or uppercase letter in the string

^[^wW] The actual character can not be a "w" or "W"






import java.util.regex.*;
import java.io.*;

public class MultipleFlag{
public static void main(String[] args){
String inputStr = "Abc\ndef";

//Simply print the original string.
System.out.println(inputStr);
String patternStr = "\n";

// Compile with multiline and case-insensitive enabled.
Pattern pattern = Pattern.compile(patternStr, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);

//Finding new line and replacing with a tab.
boolean matchFound = matcher.find();
String str = matcher.replaceAll("\t");

System.out.println(matchFound);
System.out.println(str);

//Split from where the new line character lies in the string.
String[] strg = "Abc\ndef".split("\n");
for (int i =0; i < strg.length; i++){
System.out.print(strg[i] + " ");
}
}
}


import java.util.regex.Pattern;


public class NLMatch {
public static void main(String[] argv) {

String input = "I dream of engines\nmore engines, all day long";
System.out.println("INPUT: " + input);
System.out.println();

String[] patt = { "engines.more engines", "engines$" };

for (int i = 0; i < patt.length; i++) {
System.out.println("PATTERN " + patt[i]);

boolean found;
Pattern p1l = Pattern.compile(patt[i]);
found = p1l.matcher(input).find();
System.out.println("DEFAULT match " + found);

Pattern pml = Pattern.compile(patt[i], Pattern.DOTALL
| Pattern.MULTILINE);
found = pml.matcher(input).find();
System.out.println("MultiLine match " + found);
System.out.println();
}
}
}

No comments: