Thursday, October 6, 2011

SCJP 6 Part VII - Regex

Pattern pattern = Pattern.compile("aba");
Matcher matcher = pattern.matcher("abababababababa");
while (matcher.find()) {
System.out.println("aba has been found at index : " + matcher.start() + " " + matcher.group());
}

result:

aba has been found at index : 0 aba
aba has been found at index : 4 aba
aba has been found at index : 8 aba
aba has been found at index : 12 aba

Pattern pattern1 = Pattern.compile("\\d");
Matcher matcher1 = pattern1.matcher("jatim1");
while (matcher1.find()) {
System.out.println("a digit has been found at index : " + matcher1.start() + " " + matcher1.group());
}

result:

a digit has been found at index : 5 1

Pattern pattern2 = Pattern.compile("\\s");
Matcher matcher2 = pattern2.matcher("my name is ...");
while (matcher2.find()) {
System.out.println("a space has been found at index : " + matcher2.start());
}

result:

a space has been found at index : 2
a space has been found at index : 7
a space has been found at index : 10

Pattern pattern3 = Pattern.compile("\\w");
Matcher matcher3 = pattern3.matcher("123 -> lets go");
while (matcher3.find()) {
System.out.println("a letter or a digit or an underscore has been found at index : " + matcher3.start() + " " + matcher3.group());
}

result:

a letter or a digit or an underscore has been found at index : 0 1
a letter or a digit or an underscore has been found at index : 1 2
a letter or a digit or an underscore has been found at index : 2 3
a letter or a digit or an underscore has been found at index : 7 l
a letter or a digit or an underscore has been found at index : 8 e
a letter or a digit or an underscore has been found at index : 9 t
a letter or a digit or an underscore has been found at index : 10 s
a letter or a digit or an underscore has been found at index : 12 g
a letter or a digit or an underscore has been found at index : 13 o

Pattern pattern4 = Pattern.compile("[ap]");
Matcher matcher4 = pattern4.matcher("al pacino");
while (matcher4.find()) {
System.out.println("character 'a' or 'p' has been found at index : " + matcher4.start() + " " + matcher4.group());
}


result:

character 'a' or 'p' has been found at index : 0 a
character 'a' or 'p' has been found at index : 3 p
character 'a' or 'p' has been found at index : 4 a

Pattern pattern5 = Pattern.compile("[a-l]");
Matcher matcher5 = pattern5.matcher("al pacino");
while (matcher5.find()) {
System.out.println("character 'a' until 'l' has been found at index : " + matcher5.start() + " " + matcher5.group());
}


result:

character 'a' until 'l' has been found at index : 0 a
character 'a' until 'l' has been found at index : 1 l
character 'a' until 'l' has been found at index : 4 a
character 'a' until 'l' has been found at index : 5 c
character 'a' until 'l' has been found at index : 6 i

Pattern pattern6 = Pattern.compile("[a-lA-L]");
Matcher matcher6 = pattern6.matcher("aL PaCiNo");

while (matcher6.find()) {
System.out.println("character 'a' until 'l' OR 'A' until 'L' has been found at index : " + matcher6.start() + " " + matcher6.group());
}


return:

character 'a' until 'l' OR 'A' until 'L' has been found at index : 0 a
character 'a' until 'l' OR 'A' until 'L' has been found at index : 1 L
character 'a' until 'l' OR 'A' until 'L' has been found at index : 4 a
character 'a' until 'l' OR 'A' until 'L' has been found at index : 5 C
character 'a' until 'l' OR 'A' until 'L' has been found at index : 6 i

Pattern pattern7 = Pattern.compile("\\d+");
Matcher matcher7 = pattern7.matcher("(031) 531 4249");
while (matcher7.find()) {
System.out.println("a group of digit has been found at index : " + matcher7.start() + " " + matcher7.group());
}

result (+ means one or more, \\d+ means one or more digit):

a group of digit has been found at index : 1 031
a group of digit has been found at index : 6 531
a group of digit has been found at index : 10 4249

Pattern pattern8 = Pattern.compile("0[xX]([0-9a-fA-F])+");
Matcher matcher8 = pattern8.matcher("0xffffff 0X778899 0X1 0XXX");
while (matcher8.find()) {
System.out.println("a hexadecimal has been found at index : " + matcher8.start() + " " + matcher8.group());
}

result:

a hexadecimal has been found at index : 0 0xffffff
a hexadecimal has been found at index : 9 0X778899
a hexadecimal has been found at index : 18 0X1

Pattern pattern9 = Pattern.compile("\\(\\d\\d\\d\\)([-\\s])?\\d\\d\\d([-\\s]?)\\d\\d\\d\\d");
Matcher matcher9 = pattern9.matcher("(031)-5314249, (031) 5314249, (031)5314249, (031)-531-4249, (031)-531 4249, (031) 531-42490000");
while (matcher9.find()) {
System.out.println("a valid phone number has been found at index : " + matcher9.start() + " " + matcher9.group());
}

result (? means zero or one,
([-\\s])? means zero or one either - or space):

a valid phone number has been found at index : 0 (031)-5314249
a valid phone number has been found at index : 15 (031) 5314249
a valid phone number has been found at index : 30 (031)5314249
a valid phone number has been found at index : 44 (031)-531-4249
a valid phone number has been found at index : 60 (031)-531 4249
a valid phone number has been found at index : 76 (031) 531-4249

Pattern pattern10 = Pattern.compile("[iI][nN][dD][oO]([a-zA-Z])*");
Matcher matcher10 = pattern10.matcher("Indonesia India indo123 indochina indo");
while (matcher10.find()) {
System.out.println("a string start with indo has been found at index : " + matcher10.start() + " " + matcher10.group());
}

result (* means zero or more):

a string start with indo has been found at index : 0 Indonesia
a string start with indo has been found at index : 16 indo
a string start with indo has been found at index : 24 indochina
a string start with indo has been found at index : 34 indo

Pattern pattern11 = Pattern.compile("f...");
Matcher matcher11 = pattern11.matcher("fool feel fast fame fee flash");
while (matcher11.find()) {
System.out.println(". is used to find any single char : " + matcher11.start() + " " + matcher11.group());
}

result:

. is used to find any single char : 0 fool
. is used to find any single char : 5 feel
. is used to find any single char : 10 fast
. is used to find any single char : 15 fame
. is used to find any single char : 20 fee
. is used to find any single char : 24 flas

0 comments:

 

©2009 Stay the Same | by TNB