正则表达式被无限循环卡住了(真)

时间:2018-03-02 07:52:02

标签: java regex while-loop

我刚开始java.util.regex主题。我可以执行以下代码并获得满意的结果。我在代码中使用了无限循环,试图通过给出条件来打破。然而它并没有以某种方式打破。有什么想法吗?

import java.util.Scanner;
import java.util.regex.*;
import javax.swing.JOptionPane;

public class RegEx {
    public static void main(String[] args){

        //using system input for Pattern and Matcher classes
        Scanner regex = new Scanner(System.in);

        while(true){
            System.out.println("regex please: ");
            Pattern p1 = Pattern.compile(regex.nextLine());
            System.out.println("input please: ");
            Matcher m1 = p1.matcher(regex.nextLine());
            boolean b2 = m1.matches();
            System.out.println("your input \""+m1.group()+"\" is matching and "+b2);

            if(JOptionPane.showInputDialog("does it match: ").equals("Yes")){
                System.out.println("take rest now");
                break;
            }//note sure why this condition is not working
        }
    }
}

2 个答案:

答案 0 :(得分:0)

尝试使用

            username_parameter: "login_form[username]"
            password_parameter: "login_form[password]"

答案 1 :(得分:0)

只需键入["a", "b"] 无空格,并注意["a", "", "b"]在以下弹出窗口中为大写,然后按Yes按钮。

enter image description here

执行此操作时,Y中的条件将变为OK,并且您的代码将在离开循环之前在控制台中输出字符串if,这要归功于true声明。

take rest now
相关问题