Home » Core Java (page 130)

Core Java

Simple validation example

In this example we shall show you how to make a simple validation of a String, using a Matcher against a specified Pattern. To make a simple String validation one should perform the following steps: Create a new Pattern, by compiling to it a regular expression. The regular expression constructed here is the word “Java” followed by a space character ...

Read More »

Find duplication in strings example

With this example we are going to demonstrate how to find duplication in Strings. In order to check if there are duplications in a String we have created a Matcher against a specified Pattern and used the Matcher API in boolean hasDuplicateWord(String phrase) method. In short the steps of the method are: Compile a String regular expression to a Pattern, ...

Read More »

Determine if a string matches a pattern exactly

This is an example of how to determine if a String matches to a Pattern exactly. Using a Matcher to check if a String matches to a specified Pattern implies that you should: Compile a given String regular expression to a Pattern, using compile(string regex) API method of Pattern. The given regex in the example is the character “b”. Use ...

Read More »

Matcher.appendReplacement example – Part 2

In this example we shall show you how to use Matcher.appendReplacement(StringBuffer sb, String replacement) API method to append to a StringBuffer the result of a Matcher. To use a StringBuffer to append a Matcher’s result one should perform the following steps: Compile a given String regular expression to a Pattern, using compile(string regex) API method of Pattern. Use matcher(CharSequence input) ...

Read More »

Strip extra spaces in a string

With this example we are going to demonstrate how to strip extra spaces in a String. In short, to strip extra spaces in a String you should: Use a given String with spaces between words. Use replaceAll(String regex, String replacement) API method of String, with a given regular expression. The regular expression is constructed by the white character between the ...

Read More »

Simple positive Lookbehind

This is an example of a simple positive look behind. Positive look behind methods assert the existence of a pattern to the left of the position of a String. You can form positive look behinds by opening a noncapturing group with (?<=. Positive looking behind in a String implies that you should: Compile a given String regular expression to a ...

Read More »

Find occurances of a letter in a string

In this example we shall show you how to find occurances of a specific letter in a String. To find occurances of a letter in a String one should perform the following steps: Compile a given String regular expression to a Pattern, using compile(string regex) API method of Pattern. The given regex in the example is a word boundary followed ...

Read More »

Get all digits from a string

With this example we are going to demonstrate how to get all digits from a String, using a regular expression. In short, to get all digits from a String you should: Use a given String with letters and digits. Use replaceAll(String regex, String replacement) API method of String, with a given regular expression and a String to be used as ...

Read More »

Split a string by regular expression

This is an example of how to split a String using regular expression. Splitting a String with a regular expression implies that you should: Compile a given String regular expression to a Pattern, using compile(string regex) API method of Pattern. The given regular expression in the example is the String “ian”. Use split(CharSequence input) API method of Pattern to split ...

Read More »

Parse an Apache logfile with regular expressions

In this example we shall show you how to parse an Apache logfile with regular expressions. To parse an Apache logfile with regular expressions we have followed the steps below: We have created an interface with a static final int that is the number of fields to be found and a static final String that is the log entry to ...

Read More »