1

I am trying to check if a range of numbers exist in a string Any more elegant way than this?

if (ccnumeric.contains("51")
    || ccnumeric.contains("52")
    || ccnumeric.contains("53")
    || ccnumeric.contains("54")
    || ccnumeric.contains("55"))

I can't think of any method that satisfies this as I am checking for an int range in a string.

4 Answers 4

6

You could use a simple regex: 5[12345]{1} to look for a 5 followed by exactly one 1,2,3,4 or 5:

String s = "55";
Pattern p = Pattern.compile("5[12345]{1}");
if (p.matcher(s).find()) {
    System.out.println("Found");
} else {
    System.out.println("Not Found");
}
Sign up to request clarification or add additional context in comments.

3 Comments

How about [1-5] instead of [12345]?
This will however print "Found" for three digit (or more) numbers as long as they contain one of the specified two digit numbers. For example, it would match "I have $6518 on my bank account", because there is a 51 within the 6518. I am not sure if this is really the intention (however, the code which the OP posted works in the same way, so maybe it's supposed to be like that, or it's a mistake).
@Alderath Yes I assumed the op wanted a replacement for his code. If his code is broken, mine is too ;-)
1

Maybe you can try regular expressions.

For instance

Pattern.matches( ".*5[1-5].*", ccnumeric );

Edit: To find all numbers in a string:

List<Integer> allMatches = new ArrayList<Integer>();
Matcher m = Pattern.compile("(\\d+)").matcher(ccnumeric);
while (m.find()) {
   allMatches.add(Integer.parse(m.group()));
}

1 Comment

+1 Won't work too well when the range is from 29 to 41, though.
0

Your question is a bit vague, for instance how many numbers are in the string "541", 1(541), 3(5,4,1), 6(5,4,1,54,41,541)? Depending on how you define that your regexes will change considerably. If your answer is 1, the solution is pretty straightforward, a greedy search for all numbers whose length is the same as your min and less than or equal to your max(you can then parse the matches and filter based on the min and max)...If its one of the others then your solution is going to be a bit more complicated, you are basically going to have to create a sliding window to evaluate all the possible combinations in the string.

1 Comment

i think my code explained that, but anyways it is 51-55 simple only two digits
0

I think your question is phrased ambigously... Are you looking to check if the input strings contains a number within the range 51-55? (Your written text seems to imply this) In this case, the string "512 is a power of two" would not be a match, since it does not contain the number 51.

Or are you looking to check that the string contains the character sequence 51, 52, 53, 54 or 55? (Your code seems to imply this) In this case, the string "512 is a power of two" would be a match, since it contains the character sequence 51.

If the first option is the case, then you'd need something like:

Pattern p = Pattern.compile("(^|[^\\d])5[1-5]($|[^\\d])");
if (p.matcher(inputString).find()) {
    //The inputString contains the number you seek
}

Essentially, what this is doing is looking for

  • a character sequence 51-55
  • preceeded by either the beginning of the string, or by a non-digit character
  • followed by either the end of the string, or by a non digit character

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.