1

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));
    }
}
9
  • You escaped it with \ right not //? Commented Sep 16, 2011 at 10:53
  • @Mr_Spoke, I am getting an illegal Escape character error Commented Sep 16, 2011 at 11:04
  • @dacwe, I tried that, It didnt work. The escape sequence out to be \\ Commented Sep 16, 2011 at 11:05
  • But even when I use \\ It doesn't carry out the correct logic Commented Sep 16, 2011 at 11:06
  • 1
    I really dont understand. I should probably attache the code snippet for you guys to take a look Commented Sep 16, 2011 at 11:12

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

You can test it ou in www.regex.com with the following numbers to see what i mean
23439869450 23439869450,23439869450 23439869450,23439869450,23439869450 23439869450,23439869450,23439869450,23439869450,23439869450,23439869450 07039869450,23439869450,23439869450,23439869450,23439869450,23439869450
It is supposed to accept any number that starts with (1) 234 (2) Is in the range of 10-15 in length (3) Allow commas(,) and a comma with space(, ) after the first for number for subsequent numbers
Your regex is written to mean "match a string that consists only of one number starting with 234 etc., or match pairs of such numbers if they are separated by commas".
Match pairs of numbers starting with 234... and allow to occur 1 or more times
1

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

2 Comments

ya, I mean't that. But does not seem to give an accurate logic
Just pasted the code. In the main method, tests 1-test 4 is supposed to show true while test 5 should show false since one of the numbers doesn't begin with 234

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.