Java Scanner分隔符用法 [英] Java Scanner Delimiter Usage

查看:215
本文介绍了Java Scanner分隔符用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为扫描程序指定分隔符,该分隔符在某些模式上分割,但不会从标记中删除该模式。我似乎无法使这项工作,因为正则表达式识别的任何东西也被作为分隔符的一部分被吃掉。有什么建议吗?

I'd like to specify a delimiter for a scanner that splits on some pattern, but doesn't remove that pattern from the tokens. I can't seem to make this work, as anything that is identified by the regex also gets eaten as part of the delimiter. Any suggestions?

我的具体问题,我的文件看起来像:

My specific problem, I have file that looks like:

text/numbers mix
numbers
numbers
text/numbers mix
numbers
numbers
numbers
.
.

我想从文本/数字mix +行中拆分,直到下一个文本/数字混合。我有正则表达式来识别它们,但正如所述,使用它作为分隔符吃掉我想要的部分。

I'd like to split out from the text/numbers mix+rows until the next text/numbers mix. I have the regex to identify them, but as stated, using that as the delimiter eats part of what I want.

编辑:代码添加:

static final String labelRegex="\\s*[^01\\s*]\\w+\\s*";
static final Pattern labelPattern = Pattern.compile(labelRegex, Pattern.MULTILINE);

是我用来识别文本/数字位的模式(我知道我的数字行包含所有1 / 0以空格分隔)。

is the pattern I used to identify the text/numbers bit (I know my numbers rows contain all 1/0s separated by spaces).

初始化扫描仪时:

stateScan = new Scanner(new BufferedReader(new FileReader(source)));
stateScan.useDelimiter(labelPattern);

吃标签,只留下行。我目前有一个工作实现,它在同一来源的两个缓冲文件读取器上启动两个扫描程序,一个按状态拆分,另一个按标签拆分。我真的很喜欢它只是一个抓住标签+状态。

that eats the labels, and just leaves the rows. I currently have a working implementation that starts two scanners on two buffered file readers from the same source, one splitting by states and the other by labels. I'd really like it to be just one grabbing label+state.

推荐答案

你可以在你的正则表达式中使用积极的前瞻。前方(和后方)不包含在比赛中,因此扫描仪不会吃掉它们。
这个正则表达式可能会做你想要的:

You can use a positive look ahead in your regex. Look aheads (and behinds) are not included in the match, so they won't be "eaten" by the Scanner. This regex will probably do what you want:

(?=text/numbers)

分隔符将是子字符串 text / numbers

这是一个小型演示:

public class Main {
    public static void main(String[] args) {
        String text = "text/numbers mix\n"+
                "numbers\n"+
                "numbers\n"+
                "text/numbers mix\n"+
                "numbers\n"+
                "numbers\n"+
                "numbers";
        String regex = "(?=text/numbers)";
        Scanner scan = new Scanner(text).useDelimiter(regex);
        while(scan.hasNext()) {
            System.out.println("------------------------");
            System.out.println(">"+scan.next().trim()+"<");
        }
    }
}

产生:

------------------------
>text/numbers mix
numbers
numbers<
------------------------
>text/numbers mix
numbers
numbers
numbers<

这篇关于Java Scanner分隔符用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆