user2504767
user2504767

Reputation:

Check special arrangement of specific signs in a string in Java

I need to check a string whether it includes a specific arrangements of letters and numbers.

Valid arrangements are for example:

X
X-Y
A-H-K-L-J-Y
A-H-J-Y

123
12?
12*
12-17

Invalid are for example:

-X-Y
-XY

*12
?12

I have written this method in java to solve this problem (but i don´t have some experiences with regular expressions):

public boolean checkPatternMatching(String sourceToScan, String searchPattern) {

    boolean patternFounded;

    if (sourceToScan == null) {
       patternFounded = false;
    } else {
       Pattern pattern = Pattern.compile(Pattern.quote(searchPattern),
            Pattern.CASE_INSENSITIVE);
       Matcher matcher = pattern.matcher(sourceToScan);
       patternFounded = matcher.find();
    }
    return patternFounded;
}

How can i implemented this requirement with regular expressions?

By the way: It is a good solution to check a string, whether it includes numeric content by using the method isNumeric from the java class StringUtils?

//EDIT

The link, which was edited by the admins includes not specific arrangements of characters but only an appearance of characters with regular expressions in general !

Upvotes: 2

Views: 236

Answers (2)

Julián Urbano
Julián Urbano

Reputation: 8488

After a good while trying to help, answering to constantly changing questions, just found out that the same was asked yesterday, and that the OP doesn't accept answers to his questions...all I have left to say is good night sir, good luck

n-th answer follows:

First pattern: [a-z](-[a-z])* : a letter, possibly followed by more letters, separated by -.

Second pattern: \d+(-\d+)*[?*]* : a number, possibly followed by more numbers, separated by -, and possibly ending with ? or *.

So join them together: ^([a-z](-[a-z])*)|(\d+(-\d+)*[?*]*)$. ^ and $ mark the beginning and the end of the string.

Few more comments on the code: you don't need to use Pattern.quote, and you should use matches() instead of find(), because find() returns true if any part of the string matches the pattern, and you want the whole string:

public static boolean checkPatternMatching(String sourceToScan, String searchPattern) {
    boolean patternFounded;

    if (sourceToScan == null) {
       patternFounded = false;
    } else {
       Pattern pattern = Pattern.compile(searchPattern, Pattern.CASE_INSENSITIVE);
       Matcher matcher = pattern.matcher(sourceToScan);
       patternFounded = matcher.matches();
    }
    return patternFounded;
}

Called like this: checkPatternMatching(s, "^([a-z](-[a-z])*)|(\\d+(-\\d+)*[?*]*)$")

About the second question, this is the current implementation of StringUtils.isNumeric:

public static boolean isNumeric(final CharSequence cs) {
    if (isEmpty(cs)) {
        return false;
    }
    final int sz = cs.length();
    for (int i = 0; i < sz; i++) {
        if (Character.isDigit(cs.charAt(i)) == false) {
            return false;
        }
    }
    return true;
}

So no, there is nothing wrong about it, that is as simple as it gets. But you need to include an external JAR in your program, which I find unnecessary if you just want to use such a simple method.

Upvotes: 6

Jerry
Jerry

Reputation: 71538

I believe that you should first remove the Pattern.quote() method because that would turn the inputting patterns into string literals; and those are not really useful in your context.

To match the valid arrangements with letters, something like this should work:

^[a-z](?:-[a-z])*$

For the numbers (if I understood the rules correctly):

^\\d+(?:[?*]|-\\d+)*$

And if you want to combine them:

^(?:[a-z](?:-[a-z])*|\\d+(?:[?*]|-\\d+)*)$

I'm not familiar with Java itself, nor the isNumeric method, sorry.


As per your comment, if you want to accept *12 or 1?2 or 12*456, you can use:

^\\*?\\d+(?:[?*]\\d*|-\\d+)*$

Then add it to the previous regex like so:

^(?:[a-z](?:-[a-z])*|\\*?\\d+(?:[?*]\\d*|-\\d+)*)$

Upvotes: 1

Related Questions