Java regexp any character

Regex – Match Any Character(s)

In regular expressions, we can match any character using period «.» character. To match multiple characters or a given set of characters, we should use character classes.

1. Matching a Single Character Using Regex

By default, the ‘.’ dot character in a regular expression matches a single character without regard to what character it is. The matched character can be an alphabet, a number or, any special character.

To create more meaningful patterns, we can combine the dot character with other regular expression constructs.

Pattern Description
. (Dot) Matches only a single character.
A.B Matches only a single character at second place in a 3 character long string where the string starts with ‘A’ and ends with ‘B’.
[abc] Matches only a single character from a set of given characters.
[aA] Matches only a single character ‘a’, case-insensitive.
import java.util.regex.Pattern; public class Main < public static void main(String[] args) < Pattern.compile(".").matcher("a").matches(); //true Pattern.compile(".").matcher("ab").matches(); //false Pattern.compile("A.B").matcher("AIB").matches(); //true Pattern.compile("A.B").matcher("ABI").matches(); //false Pattern.compile("A[abc]B").matcher("AaB").matches(); //true Pattern.compile("A[abc]B").matcher("AkB").matches(); //false >>

2. Matching Range of Characters

If we want to match a range of characters at any place, we need to use character classes with a hyphen between the range. e.g. ‘[a-f]’ will match a single character which can be either of ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ or ‘f’.

Читайте также:  Открыть сохранить html страницу
Pattern Description
[a-f] Matches only a single character in the range from ‘a’ to ‘f’.
[a-z] Matches only a single lowercase character in the range from ‘a’ to ‘z’.
[A-Z] Matches only a single uppercase character in the range from ‘A’ to ‘Z’.
[a-zA-Z] Matches only a single character in the range from ‘a’ to ‘z’, case-insensitive.
5 Matches only a single number in the range from ‘0’ to ‘9’.
import java.util.regex.Pattern; public class Main < public static void main(String[] args) < System.out.println(Pattern.compile("[a-f]").matcher("b").matches()); //true System.out.println(Pattern.compile("[a-f]").matcher("g").matches()); //false System.out.println(Pattern.compile("[a-zA-Z]").matcher("a").matches()); //true System.out.println(Pattern.compile("[a-zA-Z]").matcher("B").matches()); //true System.out.println(Pattern.compile("[a-zA-Z]").matcher("4").matches()); //false System.out.println(Pattern.compile("1").matcher("9").matches()); //true System.out.println(Pattern.compile("1").matcher("91").matches()); //false >>

3. Matching Multiple Characters

If we want to match a set of characters at any place then we need to use a wild card character ‘ * ‘ (asterisk) which matches 0 or more characters.

Pattern Description
.* Matches any number of characters including special characters.
5* Matches any number of digits.
[a-zA-Z]* Matches any number of alphabets.
[a-zA-Z0-9]* Matches any number of alphanumeric characters.
Pattern.compile(".*").matcher("abcd").matches(); //true Pattern.compile("[a-zA-Z]*").matcher("abcd").matches(); //true Pattern.compile("4*").matcher("01234").matches(); //true Pattern.compile("[a-zA-Z0-9]*").matcher("a1b2c3").matches(); //true

Источник

Match Any Character Using Regex In Java

In this short tutorial, we are going to shed light on how to match any character using regex in Java.

First, we will explain how to use a regular expression to match any single character. Then, we are going to showcase how to find multiple matches.

Finally, we will illustrate how to exclude and escape specific characters.

Regex to Match Any Character

Typically, we can use the dot/period pattern “.” to match a single character once.

In Java, the matched character can be any char except line terminators. However, we can address this limitation using the Pattern.DOTALL flag.

Regex to match any character

The default behavior of the dot changes depending on whether we combine it with other patterns.

For example, we used the dot pattern with the end pattern to remove the last character in a string.

Pattern Example Description
. single char except a line terminator
.? matches zero or once any character except a line terminator
.+ matches any char that is not a line terminator once or more times
.* any character (zero or more times) except a line terminator
\. matches the dot character itself
A.B a string starting with A, followed by any char, and ending with B

Basically, Java provides the Pattern class to denote a compiled regular expression.

So, let’s see how we can use it to compile a regex that matches any single character:

 @Test public void matchAnyCharacterUsingRegex() < assertTrue(Pattern.matches(".", "A")); // any char except new line assertFalse(Pattern.matches(".", "\n")); // using Pattern.DOTALL to match new line assertTrue(Pattern.compile(".", Pattern.DOTALL) .matcher("\n") .matches()); assertTrue(Pattern.matches(".?", "C")); assertFalse(Pattern.matches(".?", "CD")); assertTrue(Pattern.matches(".+", "ABC")); assertTrue(Pattern.matches(".*", "Z")); assertTrue(Pattern.matches("A.Z", "AYZ")); assertFalse(Pattern.matches("A.F", "AGH")); > 

Match Multiple Characters

The wildcard character “*“, called also asterisk, provides the easiest way to match any number of characters that are not line terminators.

For instance, we can use it with the dot ”.”, or the class “[]” patterns:

Pattern Example Description
B.*Y finds a string that starts with B, followed by any number of chars, and ends with Y
2* multiple digits only
[a-z]* matches zero or multiple lowercase alphabets
[A-Z]* only zero or multiple uppercase alphabets
[a-zA-Z]* matches any number of alphabets

Now, let’s create a test case to exemplify how to use the asterisk symbol to find any number of chars:

 @Test public void matchMultipleCharacterUsingRegex() < assertTrue(Pattern.matches("2*", "12345")); assertFalse(Pattern.matches("5*", "123ABC")); assertTrue(Pattern.matches("[a-z]*", "abcd")); assertTrue(Pattern.matches("[A-Z]*", "XYZ")); assertTrue(Pattern.matches("[a-zA-Z]*", "yzAB")); > 

Match Range of Characters

Furthermore, we can use the square brackets with a hyphen to match a range of characters.

The hyphen acts as a range delimiter as it separates the starting char and the ending char.

For instance, we can use a regex with the 6 pattern to match only numbers.

Pattern Example Description
18 matches a number between 0 and 4, followed by a number ranging from 6 to 8
[a-z]2 finds a lowercase character followed a number between 1 and 6
[c-d]2[A-N] matches a char ranging between c and d, a number between 1 and 5, and an uppercase alphabet ranging from A to N

Now, let’s demonstrate how to find a set of chars ranging between two given characters:

 @Test public void matchRangeOfCharacterUsingRegex() < assertTrue(Pattern.matches("46", "17")); assertFalse(Pattern.matches("79", "19")); assertTrue(Pattern.matches("[a-z]zhwani1", "azhwani5")); assertTrue(Pattern.matches("[a-z][A-Z]", "iN")); > 

Excluding Specific Characters

We can put the excluded characters inside the brackets prefixed by a caret [^..]. However, specifying the caret outside the brackets will mean the start of a string.

For example, [^abc] will match all chars except a, b, and c.

Please notice that the caret must be inside the brackets. Otherwise, the pattern will have another meaning.

Pattern Example Description
[^A] the character A will be excluded from the matching character
[^0-9] matches a character that is not a digit
[^A-Z] Excludes uppercase alphabets

Finally, we are going to see how to exclude characters using a regular expression in Java:

 @Test public void ExcludeCharactersUsingRegex() < assertTrue(Pattern.matches("[^a-z]", "A")); assertFalse(Pattern.matches("[^0-1]", "1")); assertTrue(Pattern.matches("[^A-Z]", "z")); > 

Escaping Special Characters

Sometimes, we want to match a character that has a special meaning in regular expressions such as dot, backslash, or caret.

To achieve this, we need to prefix the matched char with a backslash. For instance, to match a dot, we need to use the pattern “\.”.

Regex Special Characters List

Conclusion

To sum it up, in this tutorial we explained how to match any character using regex in Java.

Along the way, we have seen how to use regular expressions to match multiple chars.

Lastly, we showcased how to exclude and escape specific characters.

Liked the Article? Share it on Social media!

If you enjoy reading my articles, buy me a coffee ☕. I would be very grateful if you could consider my request ✌️

Источник

Regex Match any character in java

Regex Match any Character in java

Quick Solution for regex match any character in java

If you are looking for quick solution, you can use . character to match any character. If you need to match set of characters, then you can use .* .

Pattern Description
«.» Matches single character («.» will match with single character String such as «A»,»B»,»$» etc)
«.*» Matches any numbers of characters(«.» will match with single character String such as «AB»,»BQQQ»,»$AQ» etc)
«X.Y» Matches any character at 2nd position and String of length 3 should start with X and end with Y(«X.Y» will match for String such as «XAY»,»XRY», «X#Y» etc)
«X.*Y» Matches String which Starts with X and ends with Y.(«X.*Y» will match for String such as «XA2SY»,»X5$DY», «XOO9AY» etc)

Let’s deep dive into more about regex classes Pattern and Matcher .

Further reading:

Java regex for alphanumeric characters
Java regex for currency symbols

Introduction

Java utility package tool regex (regular expression) is often used among a vast variety of character match/search procedures, which it has two fundamental classes.

The first is the Pattern class , used to process search and locate conventional expressions. Secondly, the Matcher class is to identify any search for a defined pattern or sequence. The objective of this java utility package is to perform a search and replace action focused on text pattern(s) or characters with specific and non-specific parameters. Also, this utility package tool requires a previous base import declaration in order to operate.

Imports

In order for the regular expression utility package tool to function, there are two potential import declarations to establish. One is to declare the whole utility package tool classes alongside all its sub-classes for a general code workspace. And the other, is to declare specific utility tool sub-class(es) that focuses on a general to specific code working space. In Java, to declare an import or utility package tool to a formatted class file, the following code formats are as follows:

    Declaring the whole regex utility package tool

Each import declaration may possibly have a specific or general need(s), all dependent of the user’s scope and objective to accomplish.

Pattern and Matcher

These sub-classes can be set to create a matcher, resulting in a potential match against character sequences given by a string data type input by using the matcher() method using CharSequence as data type input.

In common cases, a typical match is a multiple match, which repeats the same character sequentially (e.g., «aaaeeeiiiooouu»). Also, the pattern class can create a given pattern given a string data type as input by using the compile() method with data parameter input regex as string data type, and flag as an integer data type (flag is to be an indicator for Case Sensitiveness, Multiline, Literal events).

The matcher() method does not have any method overloading since its only purpose is to match up a given input as character sequence versus a compiled pattern. As base case, the compiled ( Pattern.compile( input ) ) has to not be null to properly execute the design algorithm to match input and character sequence, else it will automatically attempt to call a new compile() regular expression if possible.

The compile() method does have method overloading events in order to comply with two different scenarios when called. The first one, taking as parameter a string input data type regex, compiles a new Pattern(regex); additionally, the second takes as parameters the regex string data type alongside another data type (as flag) defined as integer which functions as bit mask that includes a case insensitive (do not care for uppercase nor lowercase) , multiline events , literal events .

Match any character in a range

You can use [] with chracters to get single character in the range.
For exampe:
[b-g] : This regular expression can be used to match any single character between b and g.

Here is table for your reference:

Pattern Description
«[a-z]» Matches any character between lowercase a to z
«[A-Z]» Matches any character between uppercase A to Z
«6» Matches any character between 0 and 9
«[a-zA-Z]» Matches any character between case insensitive a to z
[bcd] Matches any character among b,c and d

Let’s understand with the help of example:

Источник

How to match any character using Java RegEx

The meta character “.” in java regular expression matches any character (single) it could be the alphabet, number or, any special character.

Example 1

import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example < public static void main(String args[]) < //Reading String from user System.out.println("Enter a String"); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); //Regular expression to match any character String regex = "."; //Compiling the regular expression Pattern pattern = Pattern.compile(regex); //Retrieving the matcher object Matcher matcher = pattern.matcher(input); int count = 0; while(matcher.find()) < count ++; >System.out.println("Given string contains "+count+" characters."); > >

Output

Enter a String hello how are you welcome to tutorialspoint Given string contains 42 characters.

You can match any 3 characters between a and b using the following regular expression −

Similarly the expression “.*” matches n number of characters.

Example 2

Following Java program reads 5 strings from the user and accepts those which starts with b, ends with a with any number of characters in between them.

import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample < public static void main( String args[] ) < String regex = "^b.*a$"; Scanner sc = new Scanner(System.in); System.out.println("Enter 5 input strings: "); String input[] = new String[5]; for (int i=0; i//Creating a Pattern object Pattern p = Pattern.compile(regex); for(int i=0; i <5;i++) < //Creating a Matcher object Matcher m = p.matcher(input[i]); if(m.find()) < System.out.println(input[i]+": accepted"); >else < System.out.println(input[i]+": not accepted"); >> > >

Output

Enter 5 input strings: barbara boolean baroda ram raju barbara: accepted boolean: not accepted baroda: accepted ram: not accepted raju: not accepted

Источник

Оцените статью