如何在java中限制匹配器或模式?

如何在java中限制匹配器或模式?,java,Java,如何限制java中的Matcher只匹配所需的字符串?下面是我尝试过的代码,但是预期的匹配应该与收到的发票类似,但它只在控制台上打印发票 import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class JavaTest { public static void main(String[] arg

如何限制java中的Matcher只匹配所需的字符串?下面是我尝试过的代码,但是预期的匹配应该与收到的发票类似,但它只在控制台上打印发票

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JavaTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List<String> actionList = new ArrayList<String>();
        actionList.add("Invoice");
        actionList.add("Invoice Received");

        List<String> notes = new ArrayList<String>();
        notes.add("Invoice Received123");

        for (String note : notes) {
            for (String action : actionList) {

                Pattern pattern = Pattern.compile(action);
                Matcher matcher = pattern.matcher(note);

                if(matcher.find()) {
                    System.out.println("Update History As : "+action);

                }
            }
        }

    }

}
这会破坏你的密码。字面上当模式匹配时,break语句退出内部for循环。因此,收到的发票从未有机会匹配

最初这是解释的问题,但后来问题变成了关于这个特定问题的流量控制。作为建议的解决方案,这里是一个没有多态性的Note对象示例,而是一个控制代码

public class Note {
    public static final int INVOICE = 1;
    public static final int INVOICE_RECEIVED = 2;

    public int noteType;
    public String userText;

    public Note(int noteType, String userText) {
        this.noteType = noteType;
        this.userText = userText;
    }

    public void doSomething() {
        switch(noteType) {
            case INVOICE:
                // do something with the INVOICE type
                break;
            case INVOICE_RECEIVED:
                // do something with the INVOICE_RECEIVED type
                break;
         }
    }

}

然后,您可以通过Note newNote=newnotnote.Invoice\u Received创建一个Invoice Received Note对象,这是一些用户文本;并将它们添加到列表中,类似于您正在做的事情,并相应地处理它们。根据笔记的数量,多态设计可能更好,或者至少更干净。但这是使用控制代码的方法。

您需要对正在查找的模式进行排序,以便一个模式的前缀始终位于该模式之后。具体而言:

List<String> actionList = new ArrayList<String>();
actionList.add("Invoice Received"); /* Make this take precedence... */
actionList.add("Invoice");          /* ... over this. */

一般来说,这种系统很容易受到bug的影响。如果您对此流程的输入有任何控制权,请对其进行修改,使便笺类型是明确的,而不是根据其内容猜测。

如果您只想搜索字符串,为什么不使用String.indexOf而不是模式匹配您搜索发票,您会找到它并打印出来,然后,您打破循环,根本不运行发票接收部分。当然,这完全是对正则表达式的滥用。@brso05它只打印发票,但我们只需要作为输出收到发票。我刚刚编辑了我的问题&我只想收到发票,请您现在检查一下好吗?您正在将发票和发票收到添加到actionsList,这是一个ArrayList。然后为每个操作循环ArrayList。因此,您可以从ArrayList中删除发票,也可以不使用循环和数组列表,只搜索一个字符串。代码的工作方式是在actionsList中搜索所有操作。正确,数组列表中有多个值,我们需要它,但根据我们在notes列表中收到的值,我们需要从actions列表中找到匹配的值,并使用该值单击update。例如,收到的发票应作为更新使用,因为我们从Matcher获得的是notesAway,任何其他方式或实施建议也可以:,您能提出一些建议吗?老实说,我不知道您希望您的代码做什么-您只希望它与收到的发票匹配,但您试图让它匹配发票和收到的发票,并输出匹配的内容?您的代码的工作方式是,它将输出actionsList中的任何匹配项,而不仅仅是第一个匹配项-中断将停止此操作,但它仍然不能保证您将找到最佳匹配项-而只是第一个匹配项。这里唯一的问题是,我们必须将note的值与action进行比较,这是因为注释部分是手动的,即根据用户的选择组合操作+文本&因此我们采用这种方式,请提供任何建议?
List<String> actionList = new ArrayList<String>();
actionList.add("Invoice Received"); /* Make this take precedence... */
actionList.add("Invoice");          /* ... over this. */
if(matcher.find()) {
  System.out.println("Update History As : "+action);
  break;
}