提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
将空格字符限制为实际空格,只需在正则表达式中使用空格即可。
import java.util.regex.Matcher; /** 来自 时 代 J a v a 公 众 号 - nowjava.com**/ import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Pattern pattern = Pattern.compile("... ..."); Matcher matcher = pattern.matcher("asd asd"); if (matcher.matches()) System.out.println("Match."); else System.out.println("Does not match."); } }