利用正则表达式匹配输入内容 ----正则表达式

前端之家收集整理的这篇文章主要介绍了利用正则表达式匹配输入内容 ----正则表达式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//利用正则表达式匹配输入内容 package regex; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexTest { public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in); System.out.println("Enter pattern:"); String patternString = in.nextLine(); Pattern pattern = Pattern.compile(patternString); while(true){ System.out.println("Enter String to match: "); String input = in.nextLine(); if(input == null || input.equals("")) return; Matcher matcher = pattern.matcher(input); if(matcher.matches()){ System.out.println("Match"); int g = matcher.groupCount(); if(g > 0){ for(int i = 0; i < input.length(); i++){ for(int j = 1; j <= g; j++){ if(i == matcher.start(j) && i == matcher.end(j)){ System.out.println("()"); } } for(int j = 1; j <= g; j++){ if(i == matcher.start(j) && i != matcher.end(j)){ System.out.println('('); } } System.out.println(input.charAt(i)); for(int j = 1; j <= g; j++){ if(i + 1 != matcher.start(j) && i + 1 == matcher.end(j)){ System.out.println(')'); } } } System.out.println(); } } else System.out.println("No match"); } } }

猜你在找的正则表达式相关文章