正则表达式以匹配我和他

时间:2017-11-26 15:30:57

标签: regex

如何使用正则表达式匹配像我这样的字符串,他,你是谁,她是 XXX' XXX

1 个答案:

答案 0 :(得分:0)

import java.util.Scanner;
public class Sol {
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        String line = sc.nextLine();
        String pattern = "[a-zA-Z]+(')[a-zA-Z]+";
        System.out.println(line.replaceAll(pattern, "xxx"));
    }
}

Input:-
I'm Light. I'm 20 years old. We''re blah.

Output:-
xxx Light. xxx 20 years old. We''re blah.

Explanation:-
The above program tries to achieve what you want to do.
Here [a-zA-Z] matches all alphabets whether small or capital
     + means that at least one alphabet should be present
     (') matches a single occurrence of '