如何将用户输入的一部分替换为另一个用户输入?

huangapple go评论71阅读模式
英文:

How to replace a part of user input with another user input?

问题

我正在制作一个需要在命令提示符下运行的文本编辑器。用户将要编辑的文本粘贴进去,然后选择他们想要对其进行的操作。我在替换粘贴的文本的某个部分方面遇到了困难。

以下是编辑器的部分代码:

import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class TextEd {
    
    public static void main(String[] args) {
        
        Editor editor = new Editor();
        editor.copiedText();
    }
}

class Editor {
    
    private Scanner scan = new Scanner(System.in);
    private String text = " ";
    
    public void copiedText() {
        System.out.println("将你的文本粘贴在这里。");        // 用户输入
        text = scan.nextLine();
        menu();
    }

    public void menu() {
        System.out.println("欢迎使用文本编辑器。\n"
            + "你想要做什么?\n"
            + "1. 替换一个单词/行。\n"
            + "2. 退出程序。");
        int choice = scan.nextInt();
    
        if (choice == 1) {
            replacing();
        }
        else if (choice == 2) {
            System.exit(0);
        }
    }
    
    // 这里是替换部分的代码,您遇到问题的地方
    public void replacing() {
        String replacement = scan.nextLine();
        System.out.println("你想要替换什么?");
        try {
            Pattern replacepat = Pattern.compile(scan.next());
            Matcher match = replacepat.matcher(text);
            System.out.println("你想要将其替换为什么?");
            scan.nextLine();
        
            boolean found = false;
            while (match.find()) {
                text = text.replaceAll(replacepat.pattern(), replacement);
                System.out.println(text);
            }
        }
        catch (Exception e) {
            System.out.println("出现了错误。");
        }
    }
}

我遇到的错误告诉我,Pattern 无法转换为 String - 我理解这一点,replaceAll 使用 int - 但我不知道如何获取用户想要替换的文本的索引,因为用户的任务是粘贴文本,然后是他们想要替换的文本的另一部分。

英文:

I'm making a text editor that has to run with cmd. The user pastes in a text they want to edit, then they choose what exactly they want to do with it. I struggle with replacing a part of the text they pasted in.

This is (a part of) my code for the editor:

import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class TextEd {
public static void main(String[] args) {
Editor editor = new Editor();
editor.copiedText();
}
}
class Editor {
private Scanner scan = new Scanner(System.in);
private String text = " ";
public void copiedText() {
System.out.println("Paste your text here.");        //The user input
text = scan.nextLine();
menu();
}
public void menu() {
System.out.println("Welcome to the text editor.\n"
+ "What do you wish to do?\n"
+ "1. Replace a word/line.\n"
+ "2. Exit program.");
int choice = scan.nextInt();
if (choice == 1) {
replacing();
}
else if (choice == 2) {
System.exit(0);
}
}
}

And here's the code for the replacing part, where I struggle:

public void replacing() {    //still not working argh
String replacement = scan.nextLine();
System.out.println("What dou you want to replace?");
try {
Pattern replacepat = Pattern.compile(scan.next());
Matcher match = replacepat.match(text);
System.out.println("What dou you want to replace it with?");
scan.nextLine();
boolean found = false;
while (match.find()) {
text = text.replaceAll(replacepat, replacement);
System.out.println(text);
}
}
catch (Exception e) { 
System.out.println("There's been an error.");
}
}

The errors I get inform me, that the Pattern cannot be converted to String - which I understand, replaceAll works with int - but I have no idea how to get the index of the text the user wants to replace, because the user's job is to paste the text in and then the other part of the text they want to replace.

答案1

得分: 0

replaceAll将以正则表达式编译第一个参数请参阅javadoc
因此您只需将正则表达式提供为字符串

public void replacing() {    //仍然无法工作,啊
        
    System.out.println("您想要替换什么?");
    try {
        String findText=scan.next();
        System.out.println("您想要用什么替换它?");
        String newText=scan.next();
    
        text = text.replaceAll(findText, newText);
        System.out.println(text);
    }
    catch (Exception e) { 
        System.out.println("出现错误。");
    }
}

来自javadoc

调用形式为str.replaceAll(regex, repl)的此方法将产生与以下表达式完全相同的结果

java.util.regex.Pattern.compile(regex).matcher(str).replaceAll(repl)
英文:

replaceAll will compile the first parameter as a regex (see javadoc)
So you just have to provide the regex as string:

public void replacing() {    //still not working argh
System.out.println("What dou you want to replace?");
try {
String findText=scan.next();
System.out.println("What dou you want to replace it with?");
String newText=scan.next();
text = text.replaceAll(findText, newText);
System.out.println(text);
}
catch (Exception e) { 
System.out.println("There's been an error.");
}
}

From javadoc:

An invocation of this method of the form str.replaceAll(regex, repl) yields 
exactly the same result as the expression 
java.util.regex.Pattern.compile(regex).matcher(str).replaceAll(repl) 

huangapple
  • 本文由 发表于 2020年10月1日 17:25:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64152482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定