一道上机题 String s="afe 5 adffz s515 e3 326 3 1g";要求打印出单词或者数字,像e3这种的既不是单词也不是数字,先谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先用str.split(" " );成数组,然后用正则表达式 就是最后打印出单词是afe adffz,数字是5 326 3 package org.luyang.lang;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;import org.apache.oro.text.regex.MalformedPatternException;import org.apache.oro.text.regex.MatchResult;import org.apache.oro.text.regex.PatternCompiler;import org.apache.oro.text.regex.PatternMatcher;import org.apache.oro.text.regex.PatternMatcherInput;import org.apache.oro.text.regex.Perl5Compiler;import org.apache.oro.text.regex.Perl5Matcher;public class RegularExpressions { public static void main(String[] args) throws MalformedPatternException { String str = "afe 5 adffz s515 e3 326 3 1g"; String[] arr = str.split("\\s+"); for (int i = 0; i < arr.length; i++){ if (arr[i].matches("\\d+") || arr[i].matches("[a-zA-Z]+")) { System.out.println(arr[i]); } }}} public class test { public static void main(String[] arg){ String s="afe 5 adffz s515 e3 326 3 1g 10er er11 er12er st20 21st 22st22"; String num=s.replaceAll("\\b\\d*[a-zA-Z]+\\d*[a-zA-A]*\\b", ""); System.out.println(num); String str=s.replaceAll("\\b[a-zA-Z]*\\d+[a-zA-Z]*\\d*\\b", ""); System.out.println(str); }} public class test { public static void main(String[] arg){ String s="afe 5 adffz s515 e3 326 3 1g 10er er11 er12er st20 21st 22st22"; String num=s.replaceAll("\\b\\d*[a-zA-Z]+\\d*[a-zA-A]*\\b", ""); System.out.println(num); String str=s.replaceAll("\\b[a-zA-Z]*\\d+[a-zA-Z]*\\d*\\b", ""); System.out.println(str); }这个不错 一个菜鸟级人物就解答 抓取论坛内容时要求先登录的问题 一个map的小问题??? 连接池问题:javax.naming.NameNotFoundException: Name jdbc is not bound in this Context 怎样给JLabel添加颜色?我菜啊~~ 一个基于TCP网络编程的问题!!! 对话框问题! NegativeArraySizeException 求JDK1.4的习题资料!非常感谢! Tomcat和Apache配置错误!请进来指教! 错误提示如下,少了什么呀 想不明白的问题!
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;import org.apache.oro.text.regex.MalformedPatternException;
import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;public class RegularExpressions {
public static void main(String[] args) throws MalformedPatternException {
String str = "afe 5 adffz s515 e3 326 3 1g";
String[] arr = str.split("\\s+");
for (int i = 0; i < arr.length; i++){
if (arr[i].matches("\\d+") || arr[i].matches("[a-zA-Z]+")) {
System.out.println(arr[i]);
}
}
}
}
public static void main(String[] arg){
String s="afe 5 adffz s515 e3 326 3 1g 10er er11 er12er st20 21st 22st22";
String num=s.replaceAll("\\b\\d*[a-zA-Z]+\\d*[a-zA-A]*\\b", "");
System.out.println(num);
String str=s.replaceAll("\\b[a-zA-Z]*\\d+[a-zA-Z]*\\d*\\b", "");
System.out.println(str);
}
}
public static void main(String[] arg){
String s="afe 5 adffz s515 e3 326 3 1g 10er er11 er12er st20 21st 22st22";
String num=s.replaceAll("\\b\\d*[a-zA-Z]+\\d*[a-zA-A]*\\b", "");
System.out.println(num);
String str=s.replaceAll("\\b[a-zA-Z]*\\d+[a-zA-Z]*\\d*\\b", "");
System.out.println(str);
}
这个不错