English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
java.util.regex.Matcher类表示执行各种匹配操作的引擎。该类没有构造函数,可以使用matches()
类java.util.regex.Pattern的方法创建/获取该类的对象。
在正则表达式中,lookbehind和lookahead构造用于匹配在某些其他模式之前或之后的特定模式。例如,如果您需要接受5到12个字符的字符串,则正则表达式为-
"\\A(?=\\w{6,10}\\z)";
默认情况下,匹配区域的边界对于向前,向后和边界匹配的结构不透明,即这些结构无法匹配区域边界之外的输入文本内容-
此类方法的hasTransparentBounds()方法验证当前匹配器是否使用透明边界,如果是,则返回true,否则返回false。
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class HasTransparentBounds { public static void main(String[] args) { //正则表达式可以接受6到10个字符 String regex = "\\A(?=\\w{6,10}\\z)"; System.out.println("Enter 5 to 12 characters: "); String input = new Scanner(System.in).next(); //创建一个模式对象 Pattern pattern = Pattern.compile(regex); //Crea un oggetto Matcher Matcher matcher = pattern.matcher(input); //Imposta l'area come stringa di input matcher.region(0, 4); if(matcher.find()) { System.out.println("Corrispondenza trovata"); } else { System.out.println("Corrispondenza non trovata"); } boolean bool = matcher.hasTransparentBounds(); //Passa a bordi trasparenti if(bool) { System.out.println("Il corrente corrispondente utilizza bordi trasparenti"); } else { System.out.println("Il corrente corrispondente utilizza bordi non trasparenti"); } } }
Risultato di output
Inserisci 5 a 12 caratteri: sampletext Match not found Current matcher user non-transparent bound
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class HasTransparentBounds { public static void main(String[] args) { //正则表达式可以接受6到10个字符 String regex = "\\A(?=\\w{6,10}\\z)"; System.out.println("Enter 5 to 12 characters: "); String input = new Scanner(System.in).next(); //创建一个模式对象 Pattern pattern = Pattern.compile(regex); //Crea un oggetto Matcher Matcher matcher = pattern.matcher(input); //Imposta l'area come stringa di input matcher.region(0, 4); matcher.useTransparentBounds(true); if(matcher.find()) { System.out.println("Corrispondenza trovata"); } else { System.out.println("Corrispondenza non trovata"); } boolean bool = matcher.hasTransparentBounds(); //Passa a bordi trasparenti if(bool) { System.out.println("Il corrente corrispondente utilizza bordi trasparenti"); } else { System.out.println("Il corrente corrispondente utilizza bordi non trasparenti"); } } }
Risultato di output
Inserisci 5 a 12 caratteri: sampletext Corrispondenza trovata Il corrente corrispondente utilizza bordi trasparenti