张学勇移民公司
微信扫一扫 分享朋友圈

已有 2260 人浏览分享

开启左侧

Java达人来看一下,wildcard 搜索问题

[复制链接]
2260 2
同城广告
地区: Winnipeg
微信: -
QQ: -
联系电话: -
Email:
在一行字“the quick brown fox jumped over the lazy dogs.”中间搜索 “the ???”, ?是通配符。
搜索的结果是:
Found 'the qui' at position 0
Found 'the laz' at position 32

不知道这一个method应该怎么写

举报 使用道具

回复

评论 2

sowen  曼省名人  发表于 2008-2-25 18:49:33 | 显示全部楼层
haven\'t really tried it but I believe the general idea is as follows:

import java.util.regex.*;

Pattern p = Pattern.compile(\"the \\w{3}\");
Matcher m = p.matcher(“the quick brown fox jumped over the lazy dogs.”);
while (m.find())
{
        // print out m.start(); // this is the index
        // print out m.toString(); // this should be the matched string
}

it\'s a much efficient way; however, there is also a simpler way, which is to just use indexOf to continue finding \"the \", blab blab blab.

举报 使用道具

回复 支持 反对
唐秋香点老虎  曼省名人  发表于 2008-2-25 18:54:50 | 显示全部楼层
有不有效率低一点的。我们还在很起源的阶段,老师让我们用indexOf之类的的方法去写。
而且我们还没有学正则表达式……

pattern不只是一个,有很多个,比如
Examining line: \'the quick brown fox jumped over the lazy dogs.\' for pattern: \'the ???\'
Found \'the qui\' at position 0
Found \'the laz\' at position 32

Examining line: \'abba is a band not from norway at all\' for pattern: \'a??a\'
Found \'abba\' at position 0
Found \'a ba\' at position 8
Found \'ay a\' at position 28
Found \'at a\' at position 31

我现在写出了确定pattern的部分了

举报 使用道具

回复 支持 反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

0

关注

1

粉丝

47

主题
精彩推荐
热门资讯
网友晒图
图文推荐

维权声明:本站有大量内容由网友产生,如果有内容涉及您的版权或隐私,请点击右下角举报,我们会立即回应和处理。
版权声明:本站也有大量原创,本站欢迎转发原创,但转发前请与本站取得书面合作协议。

Powered by Discuz! X3.4 Copyright © 2003-2020, WinnipegChinese.COM
GMT-5, 2024-5-5 16:01 , Processed in 0.018562 second(s), 29 queries .