格式化正则表达式的问题

时间:2011-09-16 10:51:00

标签: java regex string

我正在尝试将此正则表达式格式化为String模式

    (^(234\d{7,12})$)|(\b(234\d{7,12}\b\s*,\s*)(\b(234\d{7,12})\b)*)

这是一个准确的正则表达式(已在regexpal.com中验证为如此)

但是当我在java中尝试它时,它会显示错误。即使我使用//来逃避它,它仍然没有给出准确的逻辑。请。我该怎么解决这个问题呢。

package MCast;

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

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author nnanna
 */


public class Verify {

    private static final String STOP = "STOP";
    private static final String VALID = "Valid Java Identifier";
    private static final String INVALID = " Not Valid Number Format: Must be of the form 23400000023";
    private static final String VALID_IDENTIFIER_PATTERN = "(^(234\\d{7,12})$)|(\\b(234\\d{7,12}\\b\\s*,\\s*)(\\b(234\\d{7,12})\\b)*)";
//    private static final String VALID_IDENTIFIER_PATTERN2 = "[[2-3][2-3][3-4][0-9]*[ ][2-3][2-3][3-4][0-9]*]*";//[,][2[0-9]]{11}]*";
    static String str;
    boolean reply;

    public Verify() {
    }

    public int countNo(String stringToCount) {
        int j = stringToCount.length();

        int count = 0;

        for (int i = 0; i < j; i++) {
            if (stringToCount.charAt(i) == ',') {
                count += 1;
            }
        }
//        System.out.println(count);
        return count + 1;
    }

    public boolean  pattern(String str){

         Matcher match;
    Pattern pattern = Pattern.compile(VALID_IDENTIFIER_PATTERN);

            match = pattern.matcher(str);
            if (match.matches()) {
                reply = true;
                JOptionPane.showMessageDialog(null, str + ":\n" + reply + "\n" +countNo(str));
            } else {
                reply = false;
                JOptionPane.showMessageDialog(null, str + ":\n" + reply + "\n");

            }
            return reply;

    }

    public static void main(String args[]){
         Verify a = new Verify();
         String test1 = "23439869450";
         String test2 = "23439869450,23439869450";
         String test3 = "23439869450,23439869450,23439869450";
         String test4 = "23439869450,23439869450,23439869450,23439869450,23439869450,23439869450";
         String test5 = "07039869450,23439869450,23439869450,23439869450,23439869450,23439869450";
//         a.pattern(test1);
//         System.out.println(a.countNo(test1));

         a.pattern(test3);
         System.out.println(a.countNo(test2));
         System.out.println(a.pattern(test1));
         System.out.println(a.pattern(test2));
         System.out.println(a.pattern(test3));
         System.out.println(a.pattern(test4));
         System.out.println(a.pattern(test4));
//
//         a.pattern(null);
//         System.out.println(a.countNo(test1));
    }
}

2 个答案:

答案 0 :(得分:3)

你需要加倍反斜杠。你的正则表达式并没有按照你的想法去做。使用此:

Pattern regex = Pattern.compile(
    "^\n" +
    "234\\d{7,12}\\s*,\\s*234\\d{7,12}               # match a pair\n" +
    "(?:\\s*,\\s*234\\d{7,12}\\s*,\\s*234\\d{7,12})* # optionally match more pairs\n" +
    "$", 
    Pattern.COMMENTS);
Matcher regexMatcher = regex.matcher(subjectString);
foundMatch = regexMatcher.matches();

这允许以234开头的数字对; 10-15位长。所有数字必须以逗号分隔。

答案 1 :(得分:1)

您不是指\使用\\而不是//转义\\d 例如:{{1}}