首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

一道上机题,该怎么处理

2012-01-24 
一道上机题Strings afe5adffzs515e332631g 要求打印出单词或者数字,像e3这种的既不是单词也不是数字,

一道上机题
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);
}
}

热点排行