java_regex提取文本中的所有小数
package TestPrintKnowledges.Regex; import experiment9.FileName; import java.io.*; import java.nio.charset.Charset; import java.util.regex.Matcher; import java.util.regex.Pattern; public class StudyRegex { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new FileReader(FileName.fileName3, Charset.forName("utf-8"))); //BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(FileName.desktop)); String line = ""; line = (bufferedReader.readLine()); /*创建匹配模式Pattern的实例对象*/ Pattern pattern = Pattern.compile("(\\d+|)\\.\\d+");//注意,*不能单独使用(。*)可以;*要慎用,比如\\d* 不仅会匹配所有数,还包括空白符;匹配任何小数(包括省略了整数部分的小数)(用到了分组匹配) while (line != null) { /*匹配器对象:根据匹配模式类中的matcher方法,对传入的字符串进行处理,得到匹配器对象 * 其类似于迭代器啦。*/ Matcher matcher = pattern.matcher(line); /** * Creates a matcher that will match the given input against this pattern. * * @param input * The character sequence to be matched * * @return A new matcher for this pattern * * public Matcher matcher(CharSequence input) { * if (!compiled) { * synchronized(this) { * if (!compiled) * compile(); * } * } * Matcher m = new Matcher(this, input); * return m; * } */ /** * public java.util.regex.Matcher matcher(@NotNull CharSequence input) * Creates a matcher that will match the given input against this pattern. * * Params: * input – The character sequence to be matched * Returns: * A new matcher for this pattern */ //System.out.println(line); /*根据生成的匹配器对象matcher,调用其find方法,扫描该对象*/ while (matcher.find()) { /** * java.util.regex.Matcher @Contract(mutates = "this") * public boolean find() * Attempts to find the next subsequence of the input sequence that matches the pattern. * This method starts at the beginning of this matcher's region, or, if a previous invocation of the method was successful and the matcher has not since been reset, at the first character not matched by the previous match. * If the match succeeds then more information can be obtained via the start, end, and group methods. * * Returns: * true if, and only if, a subsequence of the input sequence matches this matcher's pattern */ // if(matcher.group().matches("\\S")){ } System.out.println(matcher.group()); /** * java.util.regex.Matcher @Contract(pure = true) * public String group() * Returns the input subsequence matched by the previous match. * For a matcher m with input sequence s, the expressions m.group() and s.substring(m.start(), m. end()) are equivalent. * Note that some patterns, for example a*, match the empty string. This method will return the empty string when the pattern successfully matches the empty string in the input. * * Specified by: * group in interface MatchResult * Returns: * The (possibly empty) subsequence matched by the previous match, in string form */ } line = (bufferedReader.readLine()); }//end while out bufferedReader.close(); }//end main }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2021-07-06 web_web参考手册/web参考文档