如何在一个字符串中,模糊查找一个子串,比如可以像DOS那样匹配*或?

smcdl 2003-08-22 10:55:33
如何在一个字符串中,模糊查找一个子串,比如可以像DOS那样匹配*或?
...全文
188 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackshow 2003-08-22
  • 打赏
  • 举报
回复
可以的,jdk1.4
有个新类,是关于正则表达式的,它可以进行模式匹配等
文件查找

/*
* Prints out the comments found in a .java file.
*/
import java.util.regex.*;
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
import java.nio.channels.*;

public class CharBufferExample {
public static void main(String[] args) throws Exception {
// Create a pattern to match comments
Pattern p =
Pattern.compile("//.*$", Pattern.MULTILINE);

// Get a Channel for the source file
File f = new File("Replacement.java");
FileInputStream fis = new FileInputStream(f);
FileChannel fc = fis.getChannel();

// Get a CharBuffer from the source file
ByteBuffer bb =
fc.map(FileChannel.MAP_RO, 0, (int)fc.size());
Charset cs = Charset.forName("8859_1");
CharsetDecoder cd = cs.newDecoder();
CharBuffer cb = cd.decode(bb);

// Run some matches
Matcher m = p.matcher(cb);
while (m.find())
System.out.println("Found comment: "+m.group());
}

star821116 2003-08-22
  • 打赏
  • 举报
回复
用RE吧

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧