Basic Regular expressions in java

Nwizuemmanuel
2 min readMar 24, 2021

--

Whenever you type in words in Gboard mobile keyboard those auto suggestions are powered with regular expressions and a particular computer language eg. Java.

Regular expressions is a sequence of characters that forms a search pattern.

Regular expressions can be single character, or a more complicated pattern and can be used to perform all types of text search and text replace operations.

In Java java.util.regex is a package used to work with regular expressions. The package includes the following classes:

  • Pattern class defines a pattern (to be used In a search).
  • Matcher class used to search for the pattern.
  • PatternSyntaxException class indicates syntax error in a regular expression pattern.

Example:

Visit NwizuEmmanuel at GitHub for more sample codes.

Regular expression Patterns

The first parameter of the pattern.compile() method is the pattern. It describes what is being searched for.

  • [abc]👉👉 finds one character from the options between the brackets.
  • [^abc]👉👉 finds one character NOT between the brackets.
  • [0–9]👉👉 finds one character from the range 0–9.

Note: Curly brackets are used to find a range of characters eg: [0–9]{3} == 123, 357,225 or 470 e.t.c.

Visit NwizuEmmanuel at github for sample code.

Come back for metacharacters in Java Regex(Regular expressions)👋

--

--