简体   繁体   English

Java的String .replaceFirst无法正常工作,我不知道它是否是错误或我做错了

[英]Java's String .replaceFirst is not working and I don't know if its a bug or I did it wrong

Ok I have a method that is replacing text when I use string.replace() it works but when I switch to relpaceFirst() as shown below it no longer works, what am I doing wrong or missing here? 好的,当我使用string.replace()时,我有一种替换文本的方法可以正常工作,但是当我如下图所示切换到relpaceFirst()时,它不再有效,这是我做错了还是错过了?

private void acceptAccButtonActionPerformed(java.awt.event.ActionEvent evt) {      
    int selectedAcTableItem = validAcTable.getSelectedRow();
    int selectedSugTableItem = suggestedAcTable.getSelectedRow();
    if (selectedAcTableItem > 0) {
        String acNameDefthmlText = htmlText;
        String parensName = "";
        String acName = validAcTable.getValueAt(selectedAcTableItem, 0).toString();
        String acDef = validAcTable.getValueAt(selectedAcTableItem, 1).toString();
        String acSent = validAcTable.getValueAt(selectedAcTableItem, 2).toString();
        StringBuilder acBuilder = new StringBuilder(acDef);
        acBuilder.append(" (").append(acName).append(")");            
        if (!acDef.equals("")) {
            parensName = " (" + acName + ")";
            if (htmlText.contains(acName) && !htmlText.contains(acBuilder)){
                String acReplace = acBuilder.toString();
                String acOrigDefName = acDefRow + parensName;
                if (htmlText.contains(acOrigDefName) && parensName.contains(acOrigName)){
                    acNameDefthmlText = htmlText.replaceFirst(acOrigDefName, acReplace);
                } else if (htmlText.contains(acName)) {
                    acNameDefthmlText = htmlText.replaceFirst(acName, acReplace);
                }
                htmlText = acNameDefthmlText;

            }                
            validAcTable.setValueAt(true, selectedAcTableItem, 2);
            Acronym acronym = createNewAcronym(acName, acSent, acDef, true);
            try {

                AcronymDefinitionController.sharedInstance().writeAcronymToExcelSheet(acName, acDef);
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            } catch (InvalidFormatException ex) {
                Exceptions.printStackTrace(ex);
            }
            if (validAcTable.getRowCount() - 1 >= validAcTable.getSelectedRow() + 1) {
                validAcTable.changeSelection(selectedAcTableItem + 1, 0, true, true);
            }
            validAcTable.repaint();
        }
    }

If you notice the signature of two methods in question: 如果您注意到有问题的两种方法的签名:

replace(char oldChar,char newChar);
replace(CharSequence target, CharSequence replacement);

replaceFirst(String regex, String replacement);

As you can see, in replaceFirst you matching argument is treated as regex (regular expression), which will cause the difference if any special chars are involved in the argument. 如您所见,在replaceFirst您将匹配的参数视为regex (正则表达式),如果参数中包含任何特殊字符,则将导致差异。

For example: consider below: 例如:考虑以下内容:

System.out.println("abcdab".replace("ab", "ef"));  //<- replaces all
System.out.println("abcdab".replaceFirst("ab", "ef"));//<-replaces first
System.out.println("\\abcdab".replace("\\ab", "ef")); //<-replaces first
System.out.println("\\abcdab".replaceFirst("\\ab", "ef"));
//^ doesn't replace as `\` is an special char

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不知道怎么了 - I don't know what's wrong Bug的Java。 我不知道怎么解决 - Bug java. I don't know how to fix it Java:遇到 ConcurrentModificationException。 我的迭代器和/或 HashMap 有问题,我不知道它是什么 - Java: Encountering ConcurrentModificationException. There's something wrong with my iterators and/or HashMaps, and I don't know what it is java.lang.NullPointerException,我不知道怎么了 - A java.lang.NullPointerException, and I don't know what's wrong 我的 JDBC 有问题,但我不知道为什么 - There's something wrong with my JDBC, but I don't know why 我的二叉搜索树不打印值,不知道是打印方式还是添加方式有问题 - My binary search tree won't print the values, and I don't know whether its the printing method or the adding method that's wrong 我不知道我的一个变量有什么问题。[线程“main”中的异常java.util.UnknownFormatConversionException:Conversion =&#39;m&#39;] - I don't know what's wrong with one of my variable.[Exception in thread “main” java.util.UnknownFormatConversionException: Conversion = 'm'] Java:我无法让它循环,布尔值有问题,但我不知道如何修复它 - Java: I can't get it to loop, somethings wrong with the boolean but I don't know how to fix it 我不知道BigIntegers出了什么问题 - I don't know where I am going wrong with BigIntegers 我试图制作一个程序,但控制台仍显示“线程“ main”中的异常”“ java.lang.NullPointerException”,我不知道这是怎么回事 - I've tried to make a program, but console still shows “Exception in thread ”main“ java.lang.NullPointerException” and I don't know what's wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM