Java regexp any characters

How to match «any character» in regular expression?

Yes that will work, though note that . will not match newlines unless you pass the DOTALL flag when compiling the expression:

Pattern pattern = Pattern.compile(".*123", Pattern.DOTALL); Matcher matcher = pattern.matcher(inputStr); boolean matchFound = matcher.matches(); 

That’s some very useful information! I assumed . would match newlines. I’m glad I read your answer, I need to use that!

You may also sometimes need to match newlines in Java regexes in contexts where you cannot pass Pattern.DOTALL, such as when doing a multi-line regex search in Eclipse, or as a user of any Java application that offers regex search. Based on regular-expression.info’s guide, you may need to use to match absolutely any character (the Unicode characters are additional line-terminating characters added not matched by . in Java), but just would work for most text files.

Use the pattern . to match any character once, .* to match any character zero or more times, .+ to match any character one or more times.

The most common way I have seen to encode this is with a character class whose members form a partition of the set of all possible characters.

Usually people write that as [\s\S] (whitespace or non-whitespace), though [\w\W] , [\d\D] , etc. would all work.

For reference, from regular-expressions.info/dot.html: «JavaScript and VBScript do not have an option to make the dot match line break characters. In those languages, you can use a character class such as [\s\S] to match any character. This character matches a character that is either a whitespace character (including line break characters), or a character that is not a whitespace character. Since all characters are either whitespace or non-whitespace, this character class matches 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

Читайте также:  Dom xml element php

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’.

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.
4 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("4").matcher("9").matches()); //true System.out.println(Pattern.compile("2").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.
4* 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("1*").matcher("01234").matches(); //true Pattern.compile("[a-zA-Z0-9]*").matcher("a1b2c3").matches(); //true

Источник

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

Источник

Читайте также:  Sort linked list python

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
1* 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
Читайте также:  Placing html in php

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("2*", "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 9 pattern to match only numbers.

Pattern Example Description
46 matches a number between 0 and 4, followed by a number ranging from 6 to 8
[a-z]4 finds a lowercase character followed a number between 1 and 6
[c-d]3[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("29", "19")); assertTrue(Pattern.matches("[a-z]zhwani5", "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 ✌️

Источник

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