一道上机题
String s= "afe 5 adffz s515 e3 326 3 1g ";要求打印出单词或者数字,像e3这种的既不是单词也不是数字,先谢了
------解决方案--------------------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);
}
}