Java读取行,直到Followtop [英] Java read line until followstop

查看:53
本文介绍了Java读取行,直到Followtop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我正在尝试读取包含多行的文件.为此,我正在使用scanner.nextline()

Actually, I am trying to read file which contain of multiple lines. for this I am using scanner.nextline()

但是,我要读取该行,直到followingtop(点分隔符)为止,通常在其后跟空格或行末字符.

However, I want to read the line until the followstop (dot separator) which usually followed by space or end of line char.

在这种情况下,有人可以帮助我吗?

May any body help me in this case ?

推荐答案

如果要搜索直到一个句点,可以将MatcherPattern一起使用.

If you want to search until a period, you can use a Matcher with a Pattern.

//Pattern p = Pattern.compile("[^\\.]*\\.(\\s+)"); 
Pattern p = Pattern.compile(".*?\\.(\\s+)");  //Anything any amount of times, 
                                              //followed by a dot and then some whitespace.

Matcher matcher = p.matcher("firstword. secondword.\n");

while(matcher.find()){
    boolean space = matcher.group(1).charAt(0) == ' ';
    System.out.println(matcher.start() + matcher.group() + "and is space: " + (space ? "TRUE" : "FALSE"));
}

  1. .*?-.将匹配任何内容. *匹配0次或更多次. ?惰性匹配器.这可以匹配任何类型的任意数量的字符,但是它会在第一个句点和空格之前停止(由于惰性运算符).
  2. \\.-这匹配一个句点.在Java中,必须对正则表达式中的特殊字符进行两次转义.
  3. (\\s+)-这意味着匹配空白(\s,其中包括换行)一次或多次.它与一个或多个空格字符匹配.括号捕获"了正则表达式的这一部分,因此,每当您在正则表达式上找到匹配项时,您都可以询问括号中匹配的是哪个特定部分.这可以让您知道它是空格还是换行符.
  1. .*? - . will match anything. * matches 0 or more times. ? is the lazy matcher. This matches any number of characters of any type, but it stops before the first period and whitespace (because of the lazy operator).
  2. \\. - This matches a period. In Java, you have to double escape special characters in regexes.
  3. (\\s+) - This means match whitespace (\s, which includes new lines) one or more times. It matches one or more whitespace characters. The parentheses "capture" this part of the regex so that every time you get a match on the regex you can just ask it what specific part was matched inside the parentheses. This lets you know if it is a space or a newline.

matcher.group()获取刚刚匹配的字符串.

matcher.group() gets the string that was just matched.

我添加了问号并注释了另一种模式,因为这听起来像您在某些数据中间可能有一个句点.问号会进行惰性"匹配.默认情况下,匹配是贪婪的,将采用最长的匹配字符串.因此,如果字符串中有多个位置,并带有一个句点,后跟一个空格,则它将所有内容作为一个匹配项返回.一旦到达第一个句点和空格,惰性就会强制它停止与任何字符(.*)匹配.

I added in the question mark and commented out the other pattern because it sounded like you could have a period in the middle of some of your data. The question mark does "lazy" matching. By default, matching is greedy and will take the longest matching string. So if there are multiple places in the string with a period followed by a whitespace, it will return all of that as one match. The laziness forces it to stop matching any character (.*) once it reaches the first period and space.

这篇关于Java读取行,直到Followtop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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