请教:用正则表达式从一个很长的无序的字符串中,找出所有的字母出来。?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请教:用正则表达式从一个很长的无序的字符串中,找出所有的字母出来。?相关的知识,希望对你有一定的参考价值。
String a = "str";//a表示无序字符串任意长度List<String> list = new ArrayList<String>();
String b = "";
for(int i=0;i<a.length();i++)
b = a.subString(i,i+1);//将任意长度的字符串拆分成任意个单独的字符串
Pattern p = Pattern.compile("^[a-zA-Z]$");
Matcher m = p.match(b);
if(m.find())
list.add(b);
追问
只用正则表达式筛选
追答public static void main(String[] args)
String a = "s333te444fafeafefer";//a表示无序字符串任意长度
List list = new ArrayList();
String b = "";
for(int i=0;i<a.length();i++)
b = a.substring(i, i+1);//将任意长度的字符串拆分成任意个单独的字符串
if(b.matches("^[a-zA-z]$"))
list.add(b);
System.out.println(list.size());
\\w[a-zA-Z]+$
追答你写的正则表达式也可以过滤字母啊
参考技术A $str='sadfdsfs123sdf435@dsd#$%fs^&A';$A=preg_match_all('/[a-zA-Z]1,/', $str, $matches);
PRINT_R($matches); 参考技术B str_replace("[^a-zA-Z]","","$str");
用java写一个程序,从一个很长的字符串中搜索出某一段字符,列出所有符合的字符
参考技术A import java.util.regex.Matcher;import java.util.regex.Pattern;
public class RecE
Pattern pattern;
Matcher matcher;
/* ps:
字符类
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围)
[a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集)
[a-z&&[def]] d、e 或 f(交集)
[a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去)
[a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去)
预定义字符类
. 任何字符(与行结束符可能匹配也可能不匹配)
\d 数字:[0-9]
\D 非数字: [^0-9]
\s 空白字符:[ \t\n\x0B\f\r]
\S 非空白字符:[^\s]
\w 单词字符:[a-zA-Z_0-9]
\W 非单词字符:[^\w]
*/
public void getMatchedPattern(String regx,String source)
pattern = Pattern.compile(regx);
matcher = pattern.matcher(source);
int index = 1;
while(!matcher.hitEnd())
if(matcher.find())
System.out.println("找到第"+index +"个"+matcher.group());
index++;
System.out.println("一共找到"+(index-1)+"组匹配项");
public static void main(String[] args)
new RecE().getMatchedPattern("love","ilove12what i love23e tolove dolove a efe");
结果:
找到第2个love
找到第3个love
找到第4个love
一共找到4组匹配项 参考技术B 用正则表达式,或者使用indexOf
String path = "E:\\SOFT\\Develop\\spring-framework-3.1.0.RELEASE\\dist";
String forFind = path;
String find ="soft";
ArrayList<String> temp = new ArrayList<String>();
while (true)
int index = forFind.indexOf(find);
temp.add(forFind.substring(index, index + find.length()));
forFind = forFind.substring(index + find.length());
if (index == -1 || forFind.length() < find.length())
break;
参考技术C 自己看正则表达式
就两三句代码 参考技术D 列出所有符合的字符
subStringof
以上是关于请教:用正则表达式从一个很长的无序的字符串中,找出所有的字母出来。?的主要内容,如果未能解决你的问题,请参考以下文章