1

I've to create a java regex that disable creation of databases. (I'm following these restrictions: http://dev.mysql.com/doc/refman/5.0/en/identifiers.html)

Can anyone help me to build a regex with this restriction [0-9,a-z,A-Z$_] ?

This is my snippet:

    Pattern pattern = Pattern.compile("[0-9a-zA-Z$_]");
    Matcher matcher = pattern.matcher("userdatabase");
    System.out.println(matcher.matches());
2
  • The database name can contains [0-9a-zA-Z] and $ and _ character. I don't know how create a pattern for regex. Commented Jul 18, 2011 at 11:00
  • What do you have as a regular expression? And why isn't it working? Commented Jul 18, 2011 at 11:02

3 Answers 3

3

You didn't specify a quantifier. Your expression will only match one character.

Try changing it to:

Pattern pattern = Pattern.compile("[0-9a-zA-Z$_]+");

The + indicates that it should be that expressions 1 or more times.

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

3 Comments

Thanks you, but I've to return false also when string have blank spaces ("The user") for example. Can I add this rule to pattern?
A space won't match against [0-9a-zA-Z$_], so it won't accept "The user"
Fantastic. Thanks. I'm very newbie with regex!!
0

If you mean regex for 0-9a-zA-Z$_ it is simple:

    String string = "the input";
    String regularExpression = "^[0-9a-zA-Z$_]+$";
    Pattern pattern = Pattern.compile(regularExpression);
    Matcher matcher = pattern.matcher(string);
    System.out.println(matcher.matches());

If you mean that you don't want to execute "CREATE TABLE" commands then you can check something like this:

    String regularExpression = ".*create\stable.*";
    Pattern pattern = Pattern.compile(regularExpression, Pattern.CASE_INSENSITIVE);

Comments

0

i read out of the context you want to avoid SQL injection? if yes, you could simply use prepared statements instead of sql strings.

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.