Agustin
Agustin

Reputation: 33

Translating a regex into a more basic code

How can I write the below code without using regex?

public static boolean validateCode(String code){
    boolean hasAtLeastOneNumber = Pattern.compile("[0-9].*[0-9]")
                                         .matcher(code).find();

    boolean hasAtLeastTwoLetters = Pattern.compile("[a-zA-Z].*[a-zA-Z]")
                                          .matcher(code).find();

    boolean hasAtLeastOneHyphen = Pattern.compile("-")
                                         .matcher(code).find();
}

Upvotes: 0

Views: 80

Answers (3)

Brigand
Brigand

Reputation: 86220

You can loop through the string and test it for ranges of characters. See an example on IDEONE, or ask me if you need an explanation.

import java.util.*;
import java.lang.*;

class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        System.out.println(validarCodigo("No-numbers"));
        System.out.println(validarCodigo("1-A"));
        System.out.println(validarCodigo("This 1 Matches -- :-)"));
    }

    public static boolean validarCodigo(String codigo) {
        int i;
        char[] chars = codigo.toCharArray();
        char current;
        boolean tieneAlmenosUnNumero = false;
        boolean tieneAlmenosDosLetras = false;
        boolean tieneAlmenosUnGuion = false;

        // Check for at least one number
        for (i=0; i<chars.length; i++) {
            current = chars[i];
            if (current >= '0' && current <= '9') {
                tieneAlmenosUnNumero = true;
                break;
            }
        }

        // Check for at least two letters
        int found = 0;
        for (i=0; i<chars.length; i++) {
            current = chars[i];
            boolean lower = current >= 'a' && current <= 'z';
            boolean upper = current >= 'A' && current <= 'Z';

            if (lower || upper) found++;

            if (found == 2){
                tieneAlmenosDosLetras = true;
                break;
            }
        }

        // Check for at least one hyphen
        for (i=0; i<chars.length; i++) {
            current = chars[i];
            if (current == '-') {
                tieneAlmenosUnGuion = true;
                break;
            }
        }


        return tieneAlmenosUnNumero && tieneAlmenosDosLetras && tieneAlmenosUnGuion;
    }
}

Upvotes: 1

Pshemo
Pshemo

Reputation: 124215

How about

public static boolean validateCode2(String code) {
    int numbers = 0, letters = 0, hyphens = 0;
    for (char c : code.toCharArray()) {
        if (Character.isDigit(c)) numbers++;
        if (Character.isAlphabetic(c)) letters++;
        if (c=='-') hyphens++;
    }

    return numbers>=2 && letters>=2 && hyphens>=1;
}

Upvotes: 4

wchargin
wchargin

Reputation: 16037

For hasAtLeastOneNumber:

for (char c : code.toCharArray()) {
    if (Character.isDigit(c)) {
        return true;
    }
return false;

For hasAtLeastTwoLetters:

int numFound = 0;
for (char c : code.toCharArray()) {
    if (Character.isLetter(c)) {
        numFound++;
        if (numFound >= 2) {
            return true;
        }
    }
}
return false;

For hasAtLeastOneHyphen:

for (char c : code.toCharArray()) {
    if (c == '-') {
        return true;
    }
}
return false;

If you don't want to use toCharArray, you could use:

for (int i=0; i<code.length(); i++) {
    char c = code.charAt(i);
    // do the rest of the test here
}

That's basically equivalent to using toCharArray except that it's slightly more confusing: someone who looks at the code would need to take a second or two to figure it out. With toCharArray it's obvious what you're doing.

Upvotes: 3

Related Questions