Java
/
Basics
- 1 Basics 9
-
Classes S
-
Objects S
-
Arrays S
-
Variables S
-
Loops S
-
Numbers S
-
Strings S
-
Exceptions S
-
Regexp S
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static S
-
Abstract
-
Interfaces
-
Constructors S
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text S
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable S
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread S
-
Sleep
-
Lock
-
Scheduler
- 6 Design Patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt S
- 9 Effective 7
-
Constructors S
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static S
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit S
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions S
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods S
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 13
-
Quick start S
-
Rest service S
-
Consuming rest S
-
Templates S
-
Security auth S
-
Command line S
-
Scheduled task S
-
Ajax S
-
Jdbc mysql S
-
Encrypt password S
-
Https S
-
Jwt S
-
Post request S
S
R
Q
Java Basics Regexp
Pattern matches if all the string match Use "?" modifier for ungreedy behavior p = Pattern.compile("Java?(8|SE)") m = p.matcher("Java8 JavaSE")
Matches
The Pattern matches() returns true only when preciseley matches the pattern.
/**
* Pattern, Matcher
*
* The Pattern matches() if all the string match the pattern
* To check only parts of the string find() is used
*/
package com.minte9.basics.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Matches {
public static void main(String[] args) {
Pattern p = Pattern.compile("Java");
Matcher m = p.matcher("Java SE 8");
System.out.println(m.matches()); // false
System.out.println(m.find()); // true
}
}
GREEDY
Greedy behavior (default) matches the longest sequence.
/**
* Greedy, Ungreedy
*
* Greedy behavior match the longest sequence
* Use "?" modifier for ungreedy behavior
*/
package com.minte9.basics.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Greedy {
public static void main(String[] args) {
Pattern p;
Matcher m;
p = Pattern.compile("e.+d"); // Greedy
m = p.matcher("extend cup end table");
while(m.find()) {
System.out.println(m.group()); // extend cup end
}
p = Pattern.compile("e.+?d"); // Ungreedy
m = p.matcher("extend cup end table");
while(m.find()) {
System.out.println(m.group()); // extend, end
}
p = Pattern.compile("Java ?(8|SE)"); // Ungreedy
m = p.matcher("Java 8 Java SE");
while(m.find()) {
System.out.println(m.group()); // Java 8 , Java SE
}
}
}
Replace
Replace every sequence that matches the pattern with the replacement string.
/**
* Matcher replaceAll()
*
* Replace the matches from pattern with the provided string
*/
package com.minte9.basics.regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Replace {
public static void main(String[] args) {
Pattern p = Pattern.compile("abc.*? ");
Matcher m = p.matcher("abc abcd bcd cef");
// abc, char (zero or more), ungreedy, space
while(m.find()) {
String r = m.replaceAll("AAA "); // Look Here
System.out.println(r); // AAA AAA bcd cef
}
}
}
Split
Get the text found on either side of the pattern.
/**
* Pattern, String split()
* Break the string in parts using regex pattern
* Split by char
* Split by camer case
*/
package com.minte9.basics.regexp;
import java.util.regex.Pattern;
public class Split {
public static void main(String[] args) {
String[] words;
Pattern p = Pattern.compile("[ ,.!]");
words = p.split("one two,alpha9 12!done.");
for (String w:words) {
System.out.println(w); // one two alpha9 12 done
}
words = "AbCdEf".split("(?=[A-Z])"); // Look Ahead
for(String w:words) {
System.out.println(w); // Ab Cd Ef
}
}
}
Lookaround
Lookarounds are not included in the match.T
package com.minte9.basics.regexp;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
public class LookaroundTest {
public static boolean find(String regex, String str) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
return m.find();
}
@Test public void lookBehind() {
assertTrue(find("(?<=a)x", "axyz")); // Look behind for a (true)
assertFalse(find("(?<=b)x", "axyz")); // Look befind for a (false)
}
@Test public void lookAhead() {
assertTrue(find("(?=x)xyz", "axyz")); // Look ahead for x (true)
assertFalse(find("(?=x)ax", "axyz")); // Look ahead for x (false)
}
@Test public void lookBehindNegative() {
assertFalse(find("(!?<=a)x", "ax")); // Look behind for !a (false)
}
@Test public void lookAheadNegative() {
assertFalse(find("(?!x)x", "ax")); // Look ahead for !x (false)
}
}
➥ Questions