猿问

正则表达式无法创建,错误表现如下:请问我该怎么修改?

^/[((/d{11})|((/d{3})-(/d{8})))(^1/])$
正则表达式无法创建
错误:
Illegal character range near index 22 ^/[((/d{11})|((/d{3})-(/d{8})))(^1/])$ ^

波斯汪
浏览 275回答 2
2回答

元芳怎么了

在Java中,你必须对每一个向前的斜杠(“\”)进行转义处理。图十不是Java的表示形式,所以我们要在每个“\”前面加上一个“\”以免出现编译错误。遗憾的是,转义处理过程很容易出现错误,所以应该小心谨慎。你可以首先输入未经转义处理的正则表达式,然后从左到右依次把每一个“\”替换成“\\”import org.apache.oro.text.regex.MalformedPatternException;import org.apache.oro.text.regex.MatchResult;import org.apache.oro.text.regex.Pattern;import org.apache.oro.text.regex.PatternCompiler;import org.apache.oro.text.regex.PatternMatcher;import org.apache.oro.text.regex.Perl5Compiler;import org.apache.oro.text.regex.Perl5Matcher;public class Zhengze {public static void main(String[] args) throws MalformedPatternException {PatternCompiler compiler = new Perl5Compiler();String str = ",,test,,,,test";String argexp = ".*,{3}.*";Pattern pattern = compiler.compile(argexp);PatternMatcher matcher = new Perl5Matcher();if(matcher.contains(str,pattern)) {MatchResult result = matcher.getMatch();for(int n = 0; n < result.length(); n++) {System.out.println(result.group(n));}}else{System.out.println("nothing");}}}

守候你守候我

java正则表达式就是定义某个固定格式的条件判断,之后判断某个对象是否符合这个规定的格式而出现的。举例:package com;import java.util.regex.Matcher;import java.util.regex.Pattern;public class TestRegex {public static boolean isboolIP(String ipAddress){String ip="(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})";Pattern pattern = Pattern.compile(ip);Matcher matcher = pattern.matcher(ipAddress);return matcher.matches();}/** * @param args*/public static void main(String[] args) {// TODO Auto-generated method stubString ipAddress1 = "10.";String ipAddress2 = "0.0.0.0";String ipAddress3 = "255.255.255.255";String ipAddress4 = "192.168.2.1";String ipAddress5 = "26445687";String ipAddress6 = "nihao";String ipAddress7 = "你好!!";if(isboolIP(ipAddress1)){System.out.println("IP正确");}else{System.out.println("IP错误");} if(isboolIP(ipAddress2)){System.out.println("IP正确"); }else{System.out.println("IP错误");} if(isboolIP(ipAddress3)){System.out.println("IP正确"); }else{System.out.println("IP错误");}if(isboolIP(ipAddress4)){System.out.println("IP正确"); }else{System.out.println("IP错误");
随时随地看视频慕课网APP
我要回答