How to match "any character" in regular expression?
To match "any character" in a regular expression, you can use the .
(dot) character. The .
character matches any single character, except for a newline.
For example, the regular expression "a.c"
will match the following strings:
- "abc"
- "aac"
- "a1c"
- "a#c"
Here's an example of how you can use the .
character to match any character in a regular expression:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
// Create a pattern
Pattern p = Pattern.compile("a.c");
// Create a matcher
Matcher m = p.matcher("abc");
// Check if the matcher matches the pattern
if (m.matches()) {
System.out.println("Match found!");
} else {
System.out.println("Match not found.");
}
}
}
This code will print "Match found!" because the string "abc" matches the pattern "a.c".
I hope this helps! Let me know if you have any other questions.