Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java regular expression returning false

Tags:

java

regex

I am newbie to java regular expression. I wrote following code for validating the non digit number. If we enter any non digit number it should return false. for me the below code always return false. whats the wrong here?

package regularexpression;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumberValidator {

    private static final String NUMBER_PATTERN = "\\d";
    Pattern pattern;

    public NumberValidator() {
        pattern = Pattern.compile(NUMBER_PATTERN);
    }

    public boolean validate(String line){
        Matcher matcher = pattern.matcher(line);
        return matcher.matches();
    }

    public static void main(String[] args) {

        NumberValidator validator = new NumberValidator();

        boolean validate = validator.validate("123");

        System.out.println("validate:: "+validate);
    }

}
like image 287
user414967 Avatar asked Jul 24 '12 16:07

user414967


People also ask

What is\\ w in Java?

A non-word character: [^\w] In the table above, each construct in the left-hand column is shorthand for the character class in the right-hand column. For example, \d means a range of digits (0-9), and \w means a word character (any lowercase letter, any uppercase letter, the underscore character, or any digit).

How to escape backslash in Java regex?

Characters can be escaped in Java Regex in two ways which are listed as follows which we will be discussing upto depth: Using \Q and \E for escaping. Using backslash(\\) for escaping.

How to match backslash in regex in Java?

Regular Expressions, Literal Strings and Backslashes In regular expressions, the backslash is also an escape character. The regular expression \\ matches a single backslash. This regular expression as a Java string, becomes "\\\\". That's right: 4 backslashes to match a single one.

Can regex be used in Java?

Regular expressions can be used to perform all types of text search and text replace operations. Java does not have a built-in Regular Expression class, but we can import the java. util. regex package to work with regular expressions.


2 Answers

From Java documentation:

The matches method attempts to match the entire input sequence against the pattern.

Your regular expression matches a single digit, not a number. Add + after \\d to matchone or more digits:

private static final String NUMBER_PATTERN = "\\d+";

As a side note, you can combine initialization and declaration of pattern, making the constructor unnecessary:

Pattern pattern = Pattern.compile(NUMBER_PATTERN);
like image 169
Sergey Kalinichenko Avatar answered Nov 07 '22 17:11

Sergey Kalinichenko


matches "returns true if, and only if, the entire region sequence matches this matcher's pattern."

The string is 3 digits, which doesn't match the pattern \d, meaning 'a digit'.

Instead you want the pattern \d+, meaning 'one or more digits.' This is expressed in a string as "\\d+"

like image 28
Kendall Frey Avatar answered Nov 07 '22 16:11

Kendall Frey