0% found this document useful (0 votes)
3 views5 pages

Regular Expressions

Regular Expressions (RE) are patterns used to describe text and include character classes, quantifiers, and meta-characters. In Java, RE is implemented through the String, Pattern, and Matcher classes, allowing for searching, validating, extracting, and modifying text. Key components include character classes for specific character types, quantifiers for repetition, and meta-characters for structural matching.

Uploaded by

edelisakson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Regular Expressions

Regular Expressions (RE) are patterns used to describe text and include character classes, quantifiers, and meta-characters. In Java, RE is implemented through the String, Pattern, and Matcher classes, allowing for searching, validating, extracting, and modifying text. Key components include character classes for specific character types, quantifiers for repetition, and meta-characters for structural matching.

Uploaded by

edelisakson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Regular Expressions

Regular Expressions are string patterns that


describe text
The basic patterns include character classes,
quantifiers and meta-characters

Java applies RE in following classes


String
Pattern
Matcher

RE may be used for


Searching
Validating
Extracting
Modifying
1
Character Classes
. - (Dot) Any single character
[ ] - Set of individual characters [abc] or range [a-c]
[^] - Negation of set or range [^abc] or [^a-c]
\\d - any decimal digit [0-9]
\\D - any symbol except digit [^0-9]
\\s - any whitespace symbol
\\S - any non-whitespace symbol
\\w - any alphanumeric symbol [a-zA-Z0-9_]
\\W - any non-alphanumeric symbol [^a-zA-Z0-9_]
2
Quantifiers
How many times the last specified character
can be repeated
+ - one or more
* - 0 or more
? - 0 or 1
{m} - exactly m times
{n,m} - no less than n but no more than m
{n,} - no less than n
3
Meta-Characters
| - X|Y either X or I
() -grouping matching subsequences are
remembered and numbered from left to right
beginning from 1. Example, “(KU)\1” matches
KUKU
^ - beginning of text
$ - end of text

4
Class String and Regex
matches (regex) - returns true if a string matches
a given regex
split(regex) - returns array of tokens separated by
delimiters matching regex
replaceAll(regex, substring) replace all the string’s
parts matching regex with the given substring
replaceFirst(regex, substring) replace first
encountered the string’s part matching regex
with the given substring
5

You might also like