如果我有两个字符串..说
string1="Hello dear c'Lint and dear Bob"
和
string2="dear"
我想比较字符串并删除第一次出现的匹配子字符串..
上述字符串对的结果是:
Hello c'Lint and dear Bob
这是我编写的代码,它接受输入并返回匹配的出现:
System.out.println("Enter your regex: ");
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
String RegEx = bufferRead.readLine();
Pattern pattern = Pattern.compile(RegEx);
System.out.println("Enter input string to search: ");
bufferRead = new BufferedReader(new InputStreamReader(System.in));
Matcher matcher = pattern.matcher(bufferRead.readLine());
boolean found = false;
while (matcher.find()) {
System.out.println("I found the text:\"" + matcher.group() +
"\" starting at index \'" +
matcher.start() +
"\' and ending at index \'" +
matcher.end() +
"\'");
}