bash中的正则表达式:单词类不生成匹配

时间:2016-01-10 18:39:49

标签: regex bash

我这几个小时都在苦苦思索。为什么会这样?

        btnA = new JButton("A");
    btnA.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            count += 1;
            lblTries.setText(count + " Tries"); 
            int i;
            String newName="";
            if (wordList[num].indexOf('a') > 0){
                System.out.print("Has A: ");
                for (i = -1; (i = wordList[num].indexOf("a", i + 1)) != -1; ) {
                    //System.out.print(i + " ,");

                    newName = hiddenWord.substring(0,i)+'a'+hiddenWord.substring(5);
                }                   
            }
            System.out.println(newName);
        }
    });

就好像逃避反斜杠无所作为。为什么会这样?我注意到用引号括起正则表达式并没有帮助:

                if (wordList[num].indexOf('a') >= 0){
                for (int i = -1; (i = wordList[num].indexOf("a", i + 1)) != -1; ) {
                    hiddenWord = putCharAtPlaces(hiddenWord,'a',new int[]{i});
                    lblWordDisplay.setText(hiddenWord); 
                    System.out.println(i);
                }

如果重要,这行是一个bash函数,源自{{1}}文件并在zsh上运行。

1 个答案:

答案 0 :(得分:0)

Bash代表它的角色类别(与你期望的一样)。可用文档here。以下应该有效:

if [[ "test" =~ '^[[:alnum:]]+$' ]]; then echo "yes"; else echo "no"; fi