Java
/
Basics
- 1 Basics 9
-
Classes
-
Objects
-
Arrays
-
Variables
-
Loops
-
Numbers
-
Strings
-
Exceptions
-
Regexp
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static
-
Abstract
-
Interfaces
-
Constructors
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread
-
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
- 9 Effective 7
-
Constructors
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods
-
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 8
-
Quick start
-
Rest service
-
Consuming Rest
-
Templates
-
Security
-
Command Line
-
Scheduling Tasks
-
Ajax
/
Regexp
➟
➟
Last update: 29-10-2021
Matches
! The Pattern matches() returns true only when preciseley matches the pattern.
/**
* 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.
/**
* In the followingcode you might have expect two matches:
* "extend" and "end"
*
* This is call GREEDY behavior ...
* which 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) {
// greedy
Pattern p = Pattern.compile("e.+d");
Matcher m = p.matcher("extend cup end table");
while(m.find()) {
System.out.println(m.group());
// extend cup end
}
// ungreedy
Pattern p2 = Pattern.compile("e.+?d"); // Look Here
Matcher m2 = p2.matcher("extend cup end table");
while(m2.find()) {
System.out.println(m2.group());
// extend
// end
}
}
}
Replace
You can 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");
// Match: abc, (zero or more), ungreedy, space
while(m.find()) {
String r = m.replaceAll("AAA "); // Look Here
System.out.println(r);
// AAA AAA bcd cef
}
}
}
Split
With the split() method you can get the text found on either side of the pattern.
/**
* Use Pattern split() to break the string in parts ...
* using regex pattern
*/
package com.minte9.basics.regexp;
import java.util.regex.Pattern;
public class Split {
public static void main(String[] args) {
/**
* Split by char
*/
Pattern p = Pattern.compile("[ ,.!]");
String ss[] = p.split("one two,alpha9 12!done.");
for (String s : ss) {
System.out.println(s);
// one
// two
// alpha9
// 12
// done
}
/**
* Split by camel case
*/
String s = "AbCdEf";
String[] words = s.split("(?=[A-Z])"); // Look Ahead
for(String word:words) {
System.out.println(word);
// 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 str, String regex) {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(str);
return m.find();
}
@Test public void lookBehind() {
assertTrue(find("axyz", "(?<=a)xyz")); // pass
assertFalse(find("axyz", "(?<=b)xyz")); // pass
}
@Test public void lookAhead() {
assertTrue(find("axyz", "(?=x)xyz")); // pass
assertFalse(find("axyz", "(?=x)axyz")); // pass
}
@Test public void lookBehindNegative() {
assertFalse(find("axyz", "(!?<=a)xyz")); // pass
}
@Test public void lookAheadNegative() {
assertFalse(find("axyz", "(?!x)xyz")); // pass
}
}
➥ Questions github Basics