The use of Java regular expressions in streams

Regular expressions are often very simple and convenient when matching the required characters. If you use a card, you can save a lot of codes. This article covers the content of most regular expressions, describes its basic usage and usage when processing streams.
Several tables are given below, which comprehensively list the contents of regular expressions. Selected from "Java Core Technology".
Table 2-6
Table 2-6 continued


Supplementary note: Pre-search
Pre-search
Pre-search (location assertion) can filter out the string matching the location

Here are a few examples. The
material is the first unit of College English 3:
English words
Judgment is a given mode, generally the following methods are used.

Pattern pattern=Pattern.compile("\b[a-z]+\b");
	return pattern.matcher(word).matches();

You can also use the asPredicate() method to convert the mode to a predicate to filter the stream, which is more convenient when processing files.
An example is given below: select only Chinese in the word list

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/* 单词筛选器
 * 可以筛选出英文或中文
 * 也可以筛选出指定类型的英文单词
 */
public class WordFilter
{
    
    
	
	public static void main(String[] args)
	{
    
    
		Scanner in=new Scanner(System.in);
		System.out.println("请输入原文件的绝对路径:");
		String path=in.nextLine();
		Path file=Paths.get(path);
		try(Stream<String> word=Files.readAllLines(file, StandardCharsets.UTF_8).stream())
		{
    
    
			//建立Pattern对象,它可以用来匹配字符串
			Pattern pattern=Pattern.compile("[^A-Za-z]+\\s\\D");
			
			//将模式转为谓词,过滤String流。
			List<String> result=word.filter(pattern.asPredicate()).collect(Collectors.toList());
			for(String line:result)
			{
    
    
				if(line.trim()!=null&line.trim()!=null)
				{
    
    
					System.out.println(line);
				}
			}
		}catch(IOException e)
		{
    
    
			e.printStackTrace();
		}
		finally {
    
    
			in.close();
		}
	}
}

The file is processed as follows:
Effect picture
for example, filter out words ending in ly

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/* 单词筛选器
 * 可以筛选出英文或中文
 * 也可以筛选出指定类型的英文单词
 */
public class WordFilter
{
    
    
	
	public static void main(String[] args)
	{
    
    
		Scanner in=new Scanner(System.in);
		System.out.println("请输入原文件的绝对路径:");
		String path=in.nextLine();
		Path file=Paths.get(path);
		try(Stream<String> word=Files.readAllLines(file, StandardCharsets.UTF_8).stream())
		{
    
    
			//建立Pattern对象,它可以用来匹配字符串
			Pattern pattern=Pattern.compile("\\b[A-Za-z]+ly\\b");
			
			//将模式转为谓词,过滤String流。
			List<String> result=word.filter(pattern.asPredicate()).collect(Collectors.toList());
			for(String line:result)
			{
    
    
				if(line.trim()!=null&line.trim()!=null&!line.contains(" "))
				{
    
    
					System.out.println(line);
				}
			}
		}catch(IOException e)
		{
    
    
			e.printStackTrace();
		}
		finally {
    
    
			in.close();
		}
	}
}

The effect is as follows:
Effect picture

It can be seen that regular expressions are very convenient when processing data, and they are widely used in database programming and crawler programs. If you think this article is useful, you can give the author a thumbs up!

Guess you like

Origin blog.csdn.net/m0_47202518/article/details/108176427