2 第二周(正则表达式)
× echo打印字符串。同hello命令
× grep正则匹配。
grep“hello file”,检查文件每一行,将开头是hello的行打印出来
×echo“text\nstring\nfor\ngrep" | grep “string”。 这条语句将echo后面的字符串打印内容作为grep的输入参数,会打印输出含有string的行
//第一段代码
import java.util.regex.*;
import java.util.Scanner;
public class Echohello {
public static void main(String[] args) {
String input=new String();
System.out.println("请输入hello world !或者 hello World !");
Scanner s=new Scanner(System.in);
input=s.nextLine();
String pattern="hello [Ww]orld !";
boolean isMatch =Pattern.matches(pattern,input);
//System.out.println(isMatch);
if(isMatch){
//System.out.println(isMatch);
System.out.println("world !");
}
}
}
//输出结果
请输入hello world !或者 hello World !
hello world !
world !
//第二段代码
import java.io.*;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class grep {
public static void main(String[] args) throws IOException{
System.out.println("以下是文件中的内容");
BufferedReader bfr = new BufferedReader(new FileReader("/home/noticeablyher/test/mvp.txt"));
String str=null;
int lineNumber=0;
while((str=bfr.readLine()) != null){
lineNumber++;
System.out.println(lineNumber+" "+str);
}
bfr.close();
findStringInfile("/home/noticeablyher/test/mvp.txt");
}
public static void findStringInfile(String path) throws IOException{
Scanner in=new Scanner(System.in);
System.out.println("匹配文件中以hello开头的字符串");
String pattern="hello.";
//System.out.println(pattern);
Pattern r=Pattern.compile(pattern);
File file=new File(path);
InputStreamReader read=new InputStreamReader(new FileInputStream(file));
BufferedReader bufferedReader = new BufferedReader(read);
String line=null;
int lineNumber=0;
while((line = bufferedReader.readLine()) != null){
lineNumber++;
Matcher m=r.matcher(line);
if(m.find()){
System.out.println("第"+lineNumber+"行"+"是hello开头的字符串"+line);
}
if(line.equals("hello")){
System.out.println("第"+lineNumber+"行"+"是hello开头的字符串"+line);
}
}
}
}
//输出结果
以下是文件中的内容
1 hellowordl
2 hellohello
3 wheahello
4 stringhello
5 hellostrin
6 hello
匹配文件中以hello开头的字符串
第1行是hello开头的字符串hellowordl
第2行是hello开头的字符串hellohello
第5行是hello开头的字符串hellostrin
第6行是hello开头的字符串hello
//第三段代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Pstring {
public static void main(String[] args) throws IOException {
BufferedReader bfr = new BufferedReader(new FileReader("/home/noticeablyher/test/text1.txt"));
String str=null;
int lineNumber=0;
while((str = bfr.readLine()) != null){
lineNumber++;
System.out.println(lineNumber+" "+str);
}
bfr.close();
findstringinfile("/home/noticeablyher/test/text1.txt");
}
public static void findstringinfile(String path) throws IOException{
String pattern="string";
Pattern r = Pattern.compile(pattern);
File file = new File(path);
InputStreamReader read = new InputStreamReader(new FileInputStream(file));
BufferedReader bufferedReader = new BufferedReader(read);
String line = null;
while((line = bufferedReader.readLine()) != null){
Matcher m = r.matcher(line);
if(m.find()){
System.out.println(m.group());
}
}
//输出结果
1 test
2 string
3 for
4 grep
string