QA's approach 2 Java - Regular Expressions
Regular Expressions:
abc… Letters
123… Digits
Literal escape \x
\d Any Digit
\D Any Non-digit character
. Any Character
\. Period
Grouping [...]
Union [a-e][i-u]
Intersection [a-z&&[aeiou]]
Union [a-e][i-u]
Range a-z
[a-z] Characters a to z
[0-9] Numbers 0 to 9
\w Any Alphanumeric character
\W Any Non-alphanumeric character
{m} m Repetitions
{m,n} m to n Repetitions
* Zero or more repetitions
+ One or more repetitions
? Optional character
\s Any Whitespace
\S Any Non-whitespace character
^…$ Starts and ends
(…) Capture Group
(a(bc)) Capture Sub-group
(.*) Capture all
(ab|cd) Matches ab or cd
String pattern=".ja." "Ajay"
String pattern=".*Java.*" "i am reading Java now"
. Any character (may or may not match line terminators) //Scanner sc=new Scanner(s).useDelimiter("(.)");
\d A digit: [0-9]
\D A non-digit: [^0-9]
\s A whitespace character: [ \t\n\x0B\f\r]
\S A non-whitespace character: [^\s]
\w A word character: [a-zA-Z_0-9]
\W A non-word character: [^\w] //Scanner sc=new Scanner(s).useDelimiter("[^\\w]");
Delimiter as space \\s or [ ]
// Scanner sc=new Scanner(s).useDelimiter("\\s");
// Scanner sc=new Scanner(s).useDelimiter("[ ]");
Delimiter as Tab space
//Scanner sc=new Scanner(s).useDelimiter("[\\t]");
Delimiter as Digit
//Scanner sc=new Scanner(s).useDelimiter("[\\d]");
Delimiter as 2 Digits followed by space - Union
//Scanner sc=new Scanner(s).useDelimiter("[\\d][\\d][ ]");
Character classes:
[abc] a, b, or c (simple class)
[^abc] Any character except a, b, or c (negation)
[a-zA-Z] a through z or A through Z, inclusive (range)
[a-d[m-p]] a through d, or m through p: [a-dm-p] (union)
[a-z&&[def]] d, e, or f (intersection)
[a-z&&[^bc]] a through z, except for b and c: [ad-z] (subtraction)
[a-z&&[^m-p]] a through z, and not m through p: [a-lq-z](subtraction)
abc… Letters
123… Digits
Literal escape \x
\d Any Digit
\D Any Non-digit character
. Any Character
\. Period
Grouping [...]
Union [a-e][i-u]
Intersection [a-z&&[aeiou]]
Union [a-e][i-u]
[abc] Only a, b, or c
[^abc] Not a, b, nor cRange a-z
[a-z] Characters a to z
[0-9] Numbers 0 to 9
\w Any Alphanumeric character
\W Any Non-alphanumeric character
{m} m Repetitions
{m,n} m to n Repetitions
* Zero or more repetitions
+ One or more repetitions
? Optional character
\s Any Whitespace
\S Any Non-whitespace character
^…$ Starts and ends
(…) Capture Group
(a(bc)) Capture Sub-group
(.*) Capture all
(ab|cd) Matches ab or cd
String pattern=".ja." "Ajay"
String pattern=".*Java.*" "i am reading Java now"
. Any character (may or may not match line terminators) //Scanner sc=new Scanner(s).useDelimiter("(.)");
\d A digit: [0-9]
\D A non-digit: [^0-9]
\s A whitespace character: [ \t\n\x0B\f\r]
\S A non-whitespace character: [^\s]
\w A word character: [a-zA-Z_0-9]
\W A non-word character: [^\w] //Scanner sc=new Scanner(s).useDelimiter("[^\\w]");
Delimiter as space \\s or [ ]
// Scanner sc=new Scanner(s).useDelimiter("\\s");
// Scanner sc=new Scanner(s).useDelimiter("[ ]");
Delimiter as Tab space
//Scanner sc=new Scanner(s).useDelimiter("[\\t]");
Delimiter as Digit
//Scanner sc=new Scanner(s).useDelimiter("[\\d]");
Delimiter as 2 Digits followed by space - Union
//Scanner sc=new Scanner(s).useDelimiter("[\\d][\\d][ ]");
Character classes:
[abc] a, b, or c (simple class)
[^abc] Any character except a, b, or c (negation)
[a-zA-Z] a through z or A through Z, inclusive (range)
[a-d[m-p]] a through d, or m through p: [a-dm-p] (union)
[a-z&&[def]] d, e, or f (intersection)
[a-z&&[^bc]] a through z, except for b and c: [ad-z] (subtraction)
[a-z&&[^m-p]] a through z, and not m through p: [a-lq-z](subtraction)
(Java) Java word




Pattern

Pre-Defined
character class

Quantifiers


The Java
Pattern class can be used in two ways. You can use the Pattern.matches() method to quickly
check if a text (String) matches a given regular expression. Or you can compile
a Pattern instance using Pattern.compile() which can be used
multiple times to match the regular expression against multiple texts.
Boolean
matches = matcher.matches();
If the
regular expression matches the whole text, then the matches() method returns
true. If not, the matches() method returns false.
You cannot
use the matches() method to search for multiple occurrences of a regular
expression in a text. For that, you need to use find(), start() and end()
methods.
The methods
start() and end() will give the indexes into the text where the found match
starts and ends. Actually end() returns the index of the character just after
the end of the matching section. Thus, you can use the return values of start()
and end() inside a String.substring() call.
Replaceall() method replaces
all matches of the regular expression. The replacefirst() only replaces the
first match.
Comments
Post a Comment