「匹配模式java」匹配模式加荣耀战力吗
今天给各位分享匹配模式java的知识,其中也会对匹配模式加荣耀战力吗进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java程序阅读题,求结果即可(1)
- 2、java 正则表达式matcher.group()匹配多种结果的规则
- 3、用Java验证姓名,年龄,电话怎么写。
- 4、急!!关于JAVA的simple pattern-matching
java程序阅读题,求结果即可(1)
第一题:
100
10014
100
100024
第二题:
从0到3匹配模式子序列: 0A1
从4到7匹配模式子序列: 2A3
从8到11匹配模式子序列: 4A5
从12到15匹配模式子序列: 6A7
从16到19匹配模式子序列: 8A9
***A***A***A***A***
0A1A2A3A4A5A6A7A8A9
java 正则表达式matcher.group()匹配多种结果的规则
这是由正则表达式的匹配策略所导致的,如果想要得到多个小的匹配结果你需要将正则表达式改为:
String reg = "乘+(.*?)+车";
即可得到想要的结果:
具体原理你可以查看这个链接: 讲的很详细
用Java验证姓名,年龄,电话怎么写。
1、汉字验证
这个方法改一下就行了
/**
* 计算字符串长度. 一个汉字的长度按2计算. 如果给定的字符串为null, 返回0.
*
* @param str
* 待计算长度的字符串
* @return 字符串长度
*/
public static int strlen(String str) {
if (str == null || str.length() = 0) {
return 0;
}
int len = 0;
char c;
for (int i = str.length() - 1; i = 0; i--) {
c = str.charAt(i);
if ((c = '0' c = '9') || (c = 'a' amp; c = 'z')|| (c = 'A' c = 'Z')) {//字母, 数字
len++;
} else {
if (Character.isLetter(c)) { //中文
len += 2;
} else { //符号或控制字符
len++;
}
}
}
return len;
}
2、电话
我给你个思路
就是把字符串转化为数字,然后用try catch 如果呢异常就表明是数字,至于长度length一下就行了,
或用这个
邮编号码:xxx.xxxx(都为数字)
固定电话号码:xx-xxxx-xxxx(都为数字)
手机号码:xxx-xxxx-xxxx(都为数字)
一般来说,基本的校验功能留给页面去处理,可以减轻服务器的负担。建议这些功能放在页面完成。
java中的正则表达式:
/**
* java正则表达式来判断是否EMAIL邮件
*
* @param number
* 待检测字符串
* @return 若是返回true,否则返回false
*/
import org.apache.oro.text.regex.MalformedPatternException;
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 static boolean checkFomatNumber(String number) {
//*Regexp匹配模式
//String postCodeRegexp = "([0-9]{3})+.([0-9]{4})+"; //邮政编码的匹配模式
//String phoneRegexp = "([0-9]{2})+-([0-9]{4})+-([0-9]{4})+";//固话的匹配模式
String mobileRegexp = "([0-9]{3})+-([0-9]{4})+-([0-9]{4})+"; //手机的匹配模式
PatternCompiler compiler = new Perl5Compiler();
PatternMatcher matcher = new Perl5Matcher();
Pattern pattern = null;
// Initialization of compiler, matcher, and input omitted;
try {
//pattern = compiler.compile(postCodeRegexp);
//pattern = compiler.compile(phoneRegexp);
pattern = compiler.compile(mobileRegexp);
} catch (MalformedPatternException e) {
return false;
}
if (matcher.matches(number, pattern))
return true;
else
return false;
}
3、年龄跟电话差不多,也是转换为数字,然后比较一下是否大于120
急!!关于JAVA的simple pattern-matching
//i've worked it out see as follows
import java.util.Scanner;
import java.util.regex.Pattern;
public class PatternTest {
static int am = 0;
static int bm = 0;
static int cm = 0;
static Scanner san = new Scanner(System.in);
static String sp = "[^bc]*a*[^c]*b*c*[^ab]*";
// am + bm == cm
public static boolean matchesPattern1(String input){
am = getCharNum('a', input);
bm = getCharNum('b', input);
cm = getCharNum('c', input);
return (am + bm) == cm;
}
//
public static boolean matchesPattern2(String input){
if(! matchesPattern1(input)) {
return false;
} else {
return Pattern.matches(sp, input);
}
}
public static boolean matchesPattern3(String input){
matchesPattern1(input);
if(am != cm) {
return false;
} else {
return Pattern.matches(sp, input);
}
}
public static void testAll(String input) {
System.out.println("Input: " + input);
System.out.println("matches Pattern1? : " + PatternTest.matchesPattern1(input));
System.out.println("matches Pattern2? : " + PatternTest.matchesPattern2(input));
System.out.println("matches Pattern3? : " + PatternTest.matchesPattern3(input));
}
public static void main(String[] args) {
for(int i = 0;i 5; i ++) {
System.out.print("Input String " + (i + 1) + " :");
String input = san.next();
PatternTest.testAll(input);
}
}
public static int getCharNum(char c, String input){
int num = 0;
char [] chs = input.toCharArray();
for(char ct: chs) {
if(ct == c){
num ++;
}
}
return num;
}
}
关于匹配模式java和匹配模式加荣耀战力吗的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。