Home » regex

Tag Archives: regex

MongoDB Regular Expressions Example

Hello readers, a regular expression or regex is a sequence of characters that explains a specific search pattern. In this tutorial, we will learn how to use the regular expressions in the Mongo database. 1. Introduction If you have installed the MongoDB application (version 3.6) on Windows or Ubuntu operating system and you wish to learn the regular expressions then ...

Read More »

Java 9 Regular Expressions Example

1. Introduction In this example we will explore the java.util.regex package and the abstractions contained within it that facilitate the usage of regular expressions in Java 9. Even though the common String class exports an assortment of “regex” convenience methods (eg: matches(...) & contains(...)) they are implemented in terms of the java.util.regex package and thus the focus will be on said package and ...

Read More »

Groovy Regex Example

1. Introduction Regular Expression is a character sequence defines search pattern especially for pattern matching with strings. You may see Regular Expression as Regex or Regexp in software world. In this tutorial, I will show you how to use regex operations in Groovy by using pretty easy methods.             2. Quick Start In Groovy, you ...

Read More »

Groovy Regex Example

In this example I will show you how to use regular expressions in Groovy. 1. Groovy and Regular Expressions Groovy is one of the most commonly used JVM languages and regular expressions (or simply regex) as a powerful technique to search and manipulate texts have been around us for more than a half century. As most of the languages Java ...

Read More »

java.util.regex.PatternSyntaxException Example

In this example we will discuss about java.util.regex.PatternSyntaxException. This exception is thrown when a regex (a regular exception) pattern is not correct, i.e. has syntax errors. The PatternSyntaxException extends IllegalArgumentException, which means that an argument passed to a method is illegal or inappropriate. Just like IllegalArgumentException, PatternSyntaxException is a runtime exception (indirect subclass of RuntimeException). This means that the try-catch ...

Read More »

Spring Expression Language Example

The Spring Expression Language (SpEL for short) is a powerful expression language that supports querying and manipulating an object graph at runtime. SpEL expressions can be used with XML or annotation based configuration metadata for defining BeanDefinitions. In both cases the syntax to define the expression is of the form #{ <expression string> }. We can use SpEL to inject ...

Read More »

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 »