文字中不包含数字 import java.util.regex.*; public class T1116 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String str = "2000123542120000是在工2000123542124789后面是非功过,(中国)2000134879200001后面也有文字"; Pattern p = Pattern.compile("(\\d{16})([^\\d]+)"); Matcher m = p.matcher(str); while(m.find()){ System.out.println(m.group(1)+m.group(2)); } }}//如果包含数字会出问题,看其他高手怎么解决
Vector<String> result = new Vector<String>(); for (int i = 0; i < str.length(); i++) { if (str.charAt(i) >= '0' && str.charAt(i) <='9') { for (int j = i; j < 15; j++) { if (str.charAt(i) < '0 ' || str.charAt(i) > '9') { System.out.println("格式不正确"); right = false; } else { right = true; } } } if (right) { int nextNum = 0; for (int x = i + 15; x < str.length(); x++) { if (str.charAt(x) <= '9' && str.charAt(x) >= '0') { nextNum = x; break; } } if (nextNum != 0) { result.add(str.substring(i,nextNum)); }
} i = nextNum; }
Vector<String> result = new Vector<String>(); boolean right = false; for (int i = 0; i < str.length(); i++) { right = false; if (str.charAt(i) >= '0' && str.charAt(i) <='9') { for (int j = i; j < 15; j++) { if (str.charAt(j) < '0 ' || str.charAt(j) > '9') right = false;
else right = true;
} } if (right) { int nextNum = 0; for (int x = i + 15; x < str.length(); x++) { if (str.charAt(x) <= '9' && str.charAt(x) >= '0') { nextNum = x; break; } } if (nextNum != 0) { result.add(str.substring(i,nextNum)); } } i = nextNum; }
} i = nextNum - 1; }
import java.util.regex.*; public class Test{ public static void main(String[] args){ String s = "2000123542120000是在123工2000123542124789后面是非功过,(中国)2000134879200001后面也有文字"; Pattern pa = Pattern.compile("\\d{16}[\u4e00-\u9fa5]+\\d{0,15}[\u4e00-\u9fa5]+"); Matcher ma = pa.matcher(s); while(ma.find()){ System.out.println(ma.group(0)); } } }
哈哈,不好意思,上面的那个错了,我修改了一下,这下可以了:import java.util.regex.*; public class Test{ public static void main(String[] args){ String s = "2000123542120000是在123工2000123542124789后面是非功过,(中国)2000134879200001后面也有文字"; Pattern pa = Pattern.compile("\\d{16}[^\\d]+(\\d{0,15}?)[^\\d]+"); Matcher ma = pa.matcher(s); while(ma.find()){ System.out.println(ma.group()); } } }
import java.util.regex.*;
public class T1116 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "2000123542120000是在工2000123542124789后面是非功过,(中国)2000134879200001后面也有文字";
Pattern p = Pattern.compile("(\\d{16})([^\\d]+)");
Matcher m = p.matcher(str);
while(m.find()){
System.out.println(m.group(1)+m.group(2));
} }}//如果包含数字会出问题,看其他高手怎么解决
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) >= '0' && str.charAt(i) <='9') {
for (int j = i; j < 15; j++) {
if (str.charAt(i) < '0 ' || str.charAt(i) > '9') {
System.out.println("格式不正确");
right = false;
}
else {
right = true;
}
}
}
if (right) {
int nextNum = 0;
for (int x = i + 15; x < str.length(); x++) {
if (str.charAt(x) <= '9' && str.charAt(x) >= '0') {
nextNum = x;
break;
}
}
if (nextNum != 0) {
result.add(str.substring(i,nextNum));
}
}
i = nextNum;
}
boolean right = false;
for (int i = 0; i < str.length(); i++) {
right = false;
if (str.charAt(i) >= '0' && str.charAt(i) <='9') {
for (int j = i; j < 15; j++) {
if (str.charAt(j) < '0 ' || str.charAt(j) > '9')
right = false;
else
right = true;
}
}
if (right) {
int nextNum = 0;
for (int x = i + 15; x < str.length(); x++) {
if (str.charAt(x) <= '9' && str.charAt(x) >= '0') {
nextNum = x;
break;
}
}
if (nextNum != 0) {
result.add(str.substring(i,nextNum));
} }
i = nextNum;
}
i = nextNum - 1;
}
public class Test{
public static void main(String[] args){
String s = "2000123542120000是在123工2000123542124789后面是非功过,(中国)2000134879200001后面也有文字";
Pattern pa = Pattern.compile("\\d{16}[\u4e00-\u9fa5]+\\d{0,15}[\u4e00-\u9fa5]+");
Matcher ma = pa.matcher(s);
while(ma.find()){
System.out.println(ma.group(0));
}
}
}
public class Test{
public static void main(String[] args){
String s = "2000123542120000是在123工2000123542124789后面是非功过,(中国)2000134879200001后面也有文字";
Pattern pa = Pattern.compile("\\d{16}[^\\d]+(\\d{0,15}?)[^\\d]+");
Matcher ma = pa.matcher(s);
while(ma.find()){
System.out.println(ma.group());
}
}
}