-3

I need a regular expression for:

String must be Captial letters(A-Z) and contain digits (0-9)

jzd
  • 23,473
  • 9
  • 54
  • 76
vinod
  • 1,178
  • 4
  • 16
  • 42
  • 3
    You really need to improve your style. Title is meant to describe briefly the problem : "Using regular expressions in Java for alphanumeric matching" for example. Then you can provide more details in the body of your question. Avoid to order people to give you something and explain what you already tried. We're not here to serve you but to help you. – M'vy Mar 15 '11 at 16:37
  • What do you mean by "need a code"? What shall that code do with your string? Find, replace, ...? Homework? – user unknown Mar 15 '11 at 16:37
  • Sorry for this kind of writing i agreed with ur – vinod Mar 15 '11 at 16:43
  • until you _fully_ specify what you want, we're just guessing. I suggest you kill off this question and come back when you can tell us what you want in detail. As it stands, it's deficient, even with three samples that you gave in comments. You should have specific reqs like "must begin with letter, all other characters can be letter/number/space" and so forth. – paxdiablo Mar 15 '11 at 16:45
  • @paxdiablo sry for insufficient information ur suggestion is correct .. – vinod Mar 15 '11 at 16:49

2 Answers2

1
^[A-Z]+[0-9]*$

Should match one or more capital letters followed by zero or more digits.

It would have been nice though if you:

-Told us exactly what you needed

-Gave us samples of what you expected

-What exactly have you tried...

Edit: If you want a regex that will match any string that has digits, capital letters AND white spaces without any particular order, then this should work:

^[A-Z0-9 ]*$

This will match 0 or more repetitions of capital letters, numbers and white spaces. If you want the string to contain at least a digit, whitespace or capital letter, then, this should do the trick:

^[A-Z0-9 ]+$

As opposed to the * operator which matches 0 or more repetitions, the + will match 1 or more. The ^ tells the regular expression to start matching from the very beginning of the string while the $ tells the regex to end the pattern matching at the very end of the string.

npinti
  • 51,780
  • 5
  • 72
  • 96
1

The following should work:

[A-Z0-9]+
Nick
  • 25,026
  • 7
  • 51
  • 83