0

I am trying to execute a block of code on a String if it does not contain only integers. For example, if input is 2017, nothing will happen; else if it's 2017abc, the block of code will be executed.

I have tried the regex ^[0-9]+$, but it seems like if (!keyword.matches("/^[0-9]+$/") is not working as I would like it to. I have checked multiple online sources and I'm pretty sure the regex is correct.

Am I missing something here?


Update:

Solved the problem using keywords.replaceAll("\\d", "").length() > 0. But still not sure why the above doesn't work.

Anyway, thanks to someone who suggested this answer earlier. :)

hopeweaver
  • 41
  • 5

4 Answers4

2

The workaround you stated in the update looks good. However, I would try to resolve your curiosity about why your initial code didn't work.

I tested the regex expression given in your question statement:

^[0-9]+$

And it seems to work fine for me. Based on my quick research, the problem might be in the java code that you mentioned later in your question. The slashes at the beginning and the end are not required.

Replace this

if (!keyword.matches("/^[0-9]+$/")

with this

if (!keyword.matches("^[0-9]+$")

and you are good to go. Will be happy to know if I am missing something.

For an extensive knowledge about regular expressions and pattern, I recommend the link below.

http://www.vogella.com/tutorials/JavaRegularExpressions/article.html#regular-expressions

Good luck.

Sibgha
  • 479
  • 3
  • 10
0

The correct regex may be .*[^0-9].*. If matches() returns true than do what you need to do.

LppEdd
  • 20,274
  • 11
  • 84
  • 139
0

try this

import java.util.Scanner;
    public class NotOnlyIntegers
    {
        public static void main(String[] args)
        {
            Scanner scan = new Scanner(System.in);
            System.out.println("Please enter the String");
            String test=scan.nextLine();

            int digit=0;
            int letter=0;
            for(int x=0;x<test.length()-1;++x)
            {
                if(Character.isDigit(test.charAt(x)))
                {
                    ++digit;
                }
                else if(Character.isLetter(test.charAt(x)))
                {
                    ++letter;
                }
            }
            if(digit>0&&letter>0)
            {
                System.out.println("Code Executed");
            }
            else
            System.out.println("Code Not Executed");
        }

    }
0xDEADBEEF
  • 590
  • 1
  • 5
  • 16
0

It's not pretty but why don't let Java do the job :

  private boolean isInteger(String o){
    try{
        Integer.valueOf(o);
        return true;
    }catch(NumberFormatException ex){
        return false;
    }

}
Kamil Gołąbek
  • 65
  • 1
  • 10
  • my if-statement was already in a try-catch block so i wanted to avoid using this – hopeweaver Mar 25 '17 at 13:32
  • Throwing and catching exceptions has a cost. http://stackoverflow.com/questions/36343209/which-part-of-throwing-an-exception-is-expensive – LppEdd Mar 25 '17 at 13:35