0

I am having a slight problem in Regular expressions, I would like to check if my string is in the range of 2008-13. This is were I won't to place the Regex

Array[k].startsWith("")

I have tried so far:

(200+[8-9])|(201+[0-9])

and

^20+[0-1][0-9]$

but the second one I think it will give me 2000-2019.

I would appreciate any help.

4
  • 2
    Why not parsing it as integer ? Commented Feb 6, 2013 at 9:17
  • if Array[k] returns a String then, String.startsWith() doesn't accept regex as a param . Commented Feb 6, 2013 at 9:18
  • @PremGenError, so this is the problem. Is there any other way to do it? Commented Feb 6, 2013 at 9:19
  • @dystroy because in this array[k] there are some letters as well, but in the end. Commented Feb 6, 2013 at 9:20

5 Answers 5

3

Try below logic..

public class Lottery{



    // Do not change main().
    public static void main(String [] args) throws Exception{
       String[] myArray ={"2008","2009"};
       for (String val : myArray) {
           int year = Integer.parseInt(val);
           if(year<=2013 && year>=2008)
           {
               // To do
           }
            }

    } 

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

3 Comments

Still the problem is that I cannot parse it as integer
Ok but range logic can only be applied to integers longs and related family. If you dont want to parse as Integer than you have to match each words with all the words in a for loop which is expensive.
I know, that is why I am trying to figure out the best way.
2

The regex would be:

^200[89]|201[0-3]$

but, unless you're being forced to use a single regex for validation, I'd simply parse it as an integer then check the range with an if statement.

You don't want those + characters in there since they mean one or more of the preceding objects. Hence 20+[89] would allow 200000000000000009.

1 Comment

+1 for pointing out that this should be an integer operation.
1

Your mistake is in using +. Fix your regex as following:

(200[8-9])|(201[0-9])

But better extract the year, parse it and verify as integer:

Pattern p = Pattern.compile("(\\d{4})");
Matcher m = p.matcher(str);
if (m.find()) {
    int year = Integer.parseInt(m.group(1));
    if (year > 2008  && year < 2019) {
        // do something
    }
}

This is much more robust for future modification: the year top and bottom values can be read from file, DB etc in future.

3 Comments

Alex, 0-3 for the second part of the regex.
Unfortunately I cannot parse it as integer, since I cannot transform it to integer. I think the problem is because it is string right?
I think it the closest answer:)
1

Your best shot for similar tasks would be to parse the String as Integer and compare the integers otherwise any approach you offer will be error-prone.

Still one solution that will work is:

(2008|2009|2010|2011|2012|2013)

No weird edge cases, easy to understand.

Comments

1

This is a simple regexpr (probably not the best):

20(08|09|10|11|12|13)

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.