如何将regex模式实现为整个文件而不是逐行?

时间:2021-11-04 19:26:27

How can I read from am file and find the matched patterns into a file not line by line to whole file at one time with Perl?

如何从am文件中读取匹配的模式,并在Perl中逐行逐行查找到整个文件?

1 个解决方案

#1


3  

Read the whole file into a string, by adding the local $/; handle to the beginning of the perl script before reading the file. They you can apply your regex to the resulting string.

通过添加本地$/将整个文件读入一个字符串;在读取文件之前,对perl脚本的开头句柄。可以将regex应用到结果字符串。

Once you build the long $string which contains the whole file, you can find all the matches as follows:

一旦您构建了包含整个文件的长$string,您就可以找到以下所有匹配项:

@matches = $string =~ m/\s*rem/gi;

#1


3  

Read the whole file into a string, by adding the local $/; handle to the beginning of the perl script before reading the file. They you can apply your regex to the resulting string.

通过添加本地$/将整个文件读入一个字符串;在读取文件之前,对perl脚本的开头句柄。可以将regex应用到结果字符串。

Once you build the long $string which contains the whole file, you can find all the matches as follows:

一旦您构建了包含整个文件的长$string,您就可以找到以下所有匹配项:

@matches = $string =~ m/\s*rem/gi;