Hi there, please can some help with this very simple regex. I am coming from c# and trying to get up to speed quickly, so have been trying to create samples for my refrence, however can not get this to work

String aString = "0123";
Pattern intPattern = Pattern.compile("^[0-9]{3}");
Matcher strMatch = intPattern.matcher(aString);

if(strMatch.find())
{
        System.out.println("Match");
}

For the patern I have try:
("^[0-9]$") fails
("[0-9]") fails, as if input has a no numeric character a match is still reported (as expected)
("^[0-9]\\z")fails
("\\d") fails as ("[0-9]")
("^\\d$") fails

Please please can anyone tell me what stupid mistake I am making, thank you.

you do not specify exactly what conditions are imposed on the input string

package demo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author j3c 14 IX 2009
 */
public class RegexTestProgram {

    static String[] reg = {"\\d{4}", "\\d{4}$", "^[0-9]{4}", "^[0-9]$", "[0-9]", "^[0-9]\\z",
        "\\d", "^\\d$", "\\b\\d\\d\\d\\d", "\\b\\d{4}", "^\\b\\d{4}$"};
    //inp & accept - same length
    static String[] inp = {"0123", " 0123 ", ".0123.", "a0123b", "0", "0123456", "012", "012a", "-.34"};
    static Boolean[] accept = {true, false, false, false, false, false, false, false, false};

    public static void main(String[] args) {
        int total = 0;
        int j = 0;
        for (String r : reg) {
            int counter = 0;
            System.out.println("-----------------------------------------------------------");
            System.out.println("Test pattern \t" + r + "\t" + Pattern.quote(r));
            System.out.println("-----------------------------------------------------------");
            for (int i = 0; i < accept.length; i++) {
                Pattern q = Pattern.compile(r);
                Matcher strMatch = q.matcher(inp[i]);
                if (strMatch.find()) {
                    if (accept[i]) {
                        System.out.print("positive    OK.");
                        counter++;
                    } else {
                        System.out.print("FAIL  positive ");
                    }
                } else {
                    if (!accept[i]) {
                        System.out.print("negatitive  OK.");
                        counter++;
                    } else {
                        System.out.print("FAIL negatitive");
                    }
                }
                System.out.printf("%20s%24s\n", inp[i], Pattern.quote(inp[i]));
            }
            System.out.println("j=" + j);
            System.out.println("RESULT\t" + (counter == accept.length));
            if (counter == accept.length) {
                total++;
            }
            list.add(new Calc(j, r, counter, counter == accept.length));
            j++;
            System.out.println();
        }
        ////////////////
        System.out.println("TOTAL succesed = " + total);
        Collections.sort(list);
        for (Calc it : list) {
            System.out.println(it);
        }
    }
    static List<Calc> list = new ArrayList<Calc>();

    static class Calc implements Comparable<Calc> {

        private String r;
        private int counter;
        private boolean succes;
        private int index;

        Calc(String r, int counter) {
            this.r = r;
            this.counter = counter;
        }

        private Calc(int i, String r, int counter, boolean b) {
            this(r, counter);
            succes = b;
            index = i;
        }

        public int compareTo(Calc o) {
            return Integer.valueOf(counter).compareTo(o.counter);
        }

        public String toString() {
            return String.format("for pattern[%3s] %20s     OK. = %3s   succes = %5s", index, Pattern.quote(r), counter, succes);
            // return "for pattern " + Pattern.quote(r) + " | count = " + counter + " | succes = " + succes;
        }
    }
}

see //http://www.codeproject.com/KB/dotnet/regextutorial.aspx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.