这个当然还是要用正则表达式做了 < %@ page import="java.util.regex.*"%> < % Pattern p=null; //正则表达式 Matcher m=null; //操作的字符串 boolean b; String s=null; StringBuffer sb=null; int i=0; //字符串匹配,这是不符合的 p = Pattern.compile("a*b"); m = p.matcher("baaaaab"); b = m.matches(); out.println(b+"< br>"); //字符串匹配,这是符合的
guileen(松风抚琴) 方法好,思路没看明白??
第2种情况也可以直接转INT或其他数据型,只要CATCH异常就可以了
import java.util.regex.*;
public class BasicMatch { public static void main(String[] args) { // Compile regular expression String patternStr = "b"; Pattern pattern = Pattern.compile(patternStr);
// Determine if pattern exists in input CharSequence inputStr = "a b c b"; Matcher matcher = pattern.matcher(inputStr); boolean matchFound = matcher.find(); // true
// Get matching string String match = matcher.group(); // b
// Get indices of matching string int start = matcher.start(); // 2 int end = matcher.end(); // 3 // the end is index of the last matching character + 1
// Find the next occurrence matchFound = matcher.find(); // true } }
import java.util.regex.*;public class RegexExample { public static void main(String[] args) { Pattern p = Pattern.compile("\\$(\\d+)"); Matcher m = p.matcher("it costs $4"); boolean found = m.find(); if( found ) { String foundstring = m.group(); System.out.println(foundstring);
<script language="javascript">
function on_check(){
if(form1.workid.value==""){
alert("电子卡号不能为空?");
form1.workid.focus();
return false;
}
if(form1.workid.value.length!=0&&form1.workid.value.length==8){
for(i=0;i<form1.workid.value.length;i++)
if(form1.workid.value.charAt(i)<"9"&&form1.workid.value.charAt(i)>"0")
break;
if(i==form1.workid.value.length){
alert("不能输入其它字符,只能为数字!");
form1.workid.focus();
return false;
}
}else{
alert("你输入长度不对!");
form1.workid.focus();
return false;
}
}
</script>
if (name.equals("")) {
System.out.println("不能为空");%>
<jsp:forward page="MyJsp.jsp"/>
<%}
if (name.equals("") || (name.indexOf('@') != -1)
|| (name.indexOf('.') != -1) || (name.indexOf('#') != -1)
|| (name.indexOf('¥') != -1) || (name.indexOf('%') != -1) || (name.indexOf('^') != -1)) {
System.out.println("不能输入特殊字符!");
}
else{
System.out.println("正确");
} %>
<jsp:forward page="MyJsp.jsp"/>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="4"> <tr> <td bgcolor="#000099"> <table width="100%" border="0" cellspacing="0" cellpadding="4"> <tr> <td width="100%"><font color="#CCCCCC"> <font color="#FFFFFF"><strong>Title</strong></font></font></td> </tr> </table></td> </tr> <tr> <td width="100%" bgcolor="#EAEAEA" colspan="2"> <form name="form1" id="form1" action="post.jsp"><p>
<label for="textfield">Field 1: </label> <input type="text" name="name" id="name"> </p> <p> <input type="submit" name="Submit" value="Submit"> </p> </form> </td> </tr></table>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>
My JSP 'post.jsp' starting page
</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> </head> <body>
<%String name = request.getParameter("name");
if (name.equals("")) {
System.out.println("不能为空");%>
<jsp:forward page="MyJsp.jsp"/>
<%}
if (name.equals("") || (name.indexOf('@') != -1)
|| (name.indexOf('.') != -1) || (name.indexOf('#') != -1)
|| (name.indexOf('¥') != -1) || (name.indexOf('%') != -1) || (name.indexOf('^') != -1)) {
System.out.println("不能输入特殊字符!");
}
else{
System.out.println("正确");
} %>
<jsp:forward page="MyJsp.jsp"/>
</body>
</html>
public class BasicMatch {
public static void main(String[] args) {
// Compile regular expression
String patternStr = "b";
Pattern pattern = Pattern.compile(patternStr);
// Determine if pattern exists in input
CharSequence inputStr = "a b c b";
Matcher matcher = pattern.matcher(inputStr);
boolean matchFound = matcher.find(); // true
// Get matching string
String match = matcher.group(); // b
// Get indices of matching string
int start = matcher.start(); // 2
int end = matcher.end(); // 3
// the end is index of the last matching character + 1
// Find the next occurrence
matchFound = matcher.find(); // true
}
}
{
Pattern p = Pattern.compile("\\$(\\d+)");
Matcher m = p.matcher("it costs $4"); boolean found = m.find();
if( found )
{
String foundstring = m.group();
System.out.println(foundstring);
int beginPos = m.start();
int endPos = m.end();
System.out.println("start:" + beginPos + "\nend:" + endPos); String found1 = m.group(1); // 括号内匹配内容
System.out.println(found1);
} }
}
Pattern p = Pattern.compile("\\$(\\d+)");// 自己编写的正则表达式
Matcher m = p.matcher("it costs $23");// 需要验证的字符串
boolean found = m.find();// 查找匹配的字符
if (found) {
String foundstring = m.group();// 将匹配的字符放入foundstring中
System.out.println(foundstring); int beginPos = m.start();// 匹配的第一个字符的前一位
int endPos = m.end();// 匹配字符的最后一位
System.out.println("start:" + beginPos + "\nend:" + endPos);
String found1 = m.group(1); // 括号内匹配内容
System.out.println(found1);
}
String result = m.replaceAll("¥$1");
System.out.println(result);
}
}