Week2任务

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白头若是雪可替

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值