0

I need regex that will fail only for below patterns and pass for everything else.

  1. RXXXXXXXXXX (X are digits)

  2. XXX.XXX.XXX.XXX (IP address)

I have basic knowledge of regex but not sure how to achieve this one.

For the first part, I know how to use regex to not start with R but how to make sure it allows any number of digits except 10 is not sure.

^[^R][0-9]{10}$ - it will do the !R thing but not sure how to pull off the not 10 digits part.

0

3 Answers 3

2

Well, simply define a regex:

Pattern p = Pattern.compile("R[0-9]{10} ((0|1|)[0-9]{1,2}|2([0-4][0-9]|5[0-5]))(\\.((0|1|)[0-9]{1,2}|2([0-4][0-9]|5[0-5]))){3}");
Matcher m = p.matcher(theStringToMatch);
if(!m.matches()) {
    //do something, the test didn't pass thus ok
}

Or a jdoodle.


EDIT:

Since you actually wanted two possible patterns to filter out, chance the pattern to:

Pattern p = Pattern.compile("(R[0-9]{10})|(((0|1|)[0-9]{1,2}|2([0-4][0-9]|5[0-5]))(\\.((0|1|)[0-9]{1,2}|2([0-4][0-9]|5[0-5]))){3})");

If you want to match the entire string (so that the string should start and end with the pattern, place ^ in from and $ at the end of the pattern.

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

7 Comments

Similar to @Szymon, "R1234567890 999.999.999.999" matches and the last part is not a valid IP (v4) address =\
Not anymore :P Modified it using different tracks.
Then why does the jdoodle works? Of course you need to specify a string to match...
It's returning false for R1234567890 as well as R123456789. I am looking for Not operator here.
After your last edit when changed Compile to compile, it worked :P
|
0

This should work:

!(string.matches("R\d{10}|(\d{3}\\.){3}\d{3}");

The \d means any digit, the brackets mean how many times it is repeated, and the \. means the period character. Parentheses indicate a grouping.

Here's a good reference on java regex with examples. http://www.vogella.com/tutorials/JavaRegularExpressions/article.html

Comments

-1

Regex is not meant to validate every kind of input. You could, but sometimes it is not the right approach (similar to use a wrench as a hammer: it could do it but is not meant for it).

Split the string in two parts, by the space, then validate each:

String foo = "R1234567890 255.255.255.255";
String[] stringParts = foo.split(" ");
Pattern p = Pattern.compile("^[^R][0-9]{10}$");
Matcher m = p.macher(stringParts[0]);
if (m.matches()) {
    //the first part is valid
    //start validating the IP
    String[] ipParts = stringParts.split("\\.");
    for (String ip : ipParts) {
        int ipPartValue = Integer.parseInt(ip);
        if (!(ipPartValue >= 0 && ipPartValue <= 255)) {
            //error...
        }
    }
}

Comments

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.