nnanna
nnanna

Reputation: 287

Problem with formatting a regular expression

I am trying to format this regular expression into a String pattern

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

This is an accurate regex (a has been validated in regexpal.com as being so)

But when I try it in java, it shows errors. And even if I escape it using //, It still dosen't give an accurate Logic. Please. How can I solve this.

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));
    }
}

Upvotes: 1

Views: 126

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336078

You need to double the backslashes. And your regex isn't doing what you think it is. Use this:

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();

This allows pairs of numbers starting with 234; 10-15 digits long. All numbers must be comma-separated.

Upvotes: 3

epoch
epoch

Reputation: 16595

don't you mean escaping the \ with \\ and not // ex : \\d

Upvotes: 1

Related Questions