How to match any single character in a string using regular expression with Java

1 Answer

0 votes
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Main {
    public static void main(String[] args) {
        String patternString = "b.d";
        String[] testStrings = {"bud", "bid", "bed", "b d", "bat", "bd", "bead"};

        Pattern pattern = Pattern.compile(patternString);

        for (String testString : testStrings) {
            Matcher matcher = pattern.matcher(testString);
            if (matcher.find()) {
                System.out.println(1);
            } else {
                System.out.println(0);
            }
        }
    }
}


/*
run:

1
1
1
1
0
0
0

*/

 



answered Feb 15, 2025 by avibootz
...