免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1759 | 回复: 3
打印 上一主题 下一主题

两个基础知识问题,谢谢大家 [复制链接]

论坛徽章:
0
1 [报告]
发表于 2008-01-25 15:58 |只看该作者
1. 可以看看Matcher.find()的源代码就知道了,这个类保有 from 和 to两个和输入相关的值,是从位置from一直匹配到位置to。
public boolean find() {
        int nextSearchIndex = last;
        if (nextSearchIndex == first)
            nextSearchIndex++;

        // If next search starts before region, start it at region
        if (nextSearchIndex < from)
            nextSearchIndex = from;

        // If next search starts beyond region then it fails
        if (nextSearchIndex > to) {
            for (int i = 0; i < groups.length; i++)
                groups = -1;
            return false;
        }
        return search(nextSearchIndex);
    }

2. 就当书上说的放屁好了,自己能解释的通就可以了

论坛徽章:
0
2 [报告]
发表于 2008-01-25 16:03 |只看该作者
Formatter的代码

        private void printString(Object arg, Locale l) throws IOException {
            if (arg == null) {
                print("null");
            } else if (arg instanceof Formattable) {
                Formatter fmt = formatter;
                if (formatter.locale() != l)
                    fmt = new Formatter(formatter.out(), l);
                ((Formattable)arg).formatTo(fmt, f.valueOf(), width, precision);
            } else {
                print(arg.toString());  -----最后调用了obj.toString()
            }
        }

论坛徽章:
0
3 [报告]
发表于 2008-01-28 16:11 |只看该作者
对于第二个问题我明白了,呵呵。多谢

论坛徽章:
0
4 [报告]
发表于 2008-01-28 16:17 |只看该作者
对于贪婪*来说,我做了几个实验


第一,去掉*,将匹配符修改为\\w
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Regex2 {

        public static void main(String[] args) {
                Pattern p = Pattern.compile( "\\w" );
                Matcher m = p.matcher( "ab34ef" );
               
                boolean b = false;
               
                int i = 0;
                while ( b = m.find() ){
                        
                        System.out.println("///////////////////////////////");
                        
                        System.out.println("Time is " + ++ i);
                        System.out.println( "m.start is " + m.start() );
                        System.out.println( "m.end is " + m.end() );
                        System.out.println( "m.group is " + m.group() );
                        System.out.println("///////////////////////////////");
                }
        }
}

结果是这样
///////////////////////////////
Time is 1
m.start is 0
m.end is 1
m.group is a
///////////////////////////////
///////////////////////////////
Time is 2
m.start is 1
m.end is 2
m.group is b
///////////////////////////////
///////////////////////////////
Time is 3
m.start is 2
m.end is 3
m.group is 3
///////////////////////////////
///////////////////////////////
Time is 4
m.start is 3
m.end is 4
m.group is 4
///////////////////////////////
///////////////////////////////
Time is 5
m.start is 4
m.end is 5
m.group is e
///////////////////////////////
///////////////////////////////
Time is 6
m.start is 5
m.end is 6
m.group is f
///////////////////////////////

如果修改为\\w*,则结果是这样

///////////////////////////////
Time is 1
m.start is 0
m.end is 6
m.group is ab34ef
///////////////////////////////
///////////////////////////////
Time is 2
m.start is 6
m.end is 6
m.group is
///////////////////////////////

所以我忽略了一个平时容易疏忽的问题,就是两个索引确定一个字符的概念。

     a   b  3  4  e  f
    0  1  2  3  4  5  6

我猜测,当贪婪匹配量词存在的情况下,字符串末尾的""(就是空字符)也会被扫描

一下是api的原话

group
public String group()返回由以前匹配操作所匹配的输入子序列。
对于具有输入序列 s 的匹配器 m,表达式 m.group() 和 s.substring(m.start(), m.end()) 是等效的。

注意,某些模式(例如,a*)匹配空字符串。当模式成功匹配输入中的空字符串时,此方法将返回空字符串。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP