1

I am trying to check the entered String is alphanumeric or not.I tried some regular expressions i.e.,

 ^[a-zA-Z0-9]+$
 ^[A-Za-z0-9]*$
 ^[\pL\pN]+$

But all these will work if the String contains only numeric or contains only alphabets or alphanumeric. But I need to check the String should be AlphaNumeric and Starts with a digit only. Please help me out.

7
  • You can do a try-catch and parse your value to an int or double. If it fails and throws an exception you know it's not a numeric value. Commented May 30, 2014 at 12:07
  • It appears you want to add the requirement that it start with a digit. So I would add a digit to the start of your regex. What did you try to meet this requirement and what problem are you having? Commented May 30, 2014 at 12:07
  • Your question is unclear. You should post some examples of input and expected results with explanation why such results are expected (why they are correct/incorrect)? Commented May 30, 2014 at 12:15
  • Note: AlphaNumeric usually means containing any combination of letters or digits and can be only letters or digits. Commented May 30, 2014 at 12:19
  • IMO an upvote isn't warranted on a regex question like this since (a) exact examples are easily searchable, and (b) the OP clearly hasn't tried very hard to find an answer. While a downvote might be too much, there is little redeeming value given the ambiguous wording and lack of followup. Commented May 30, 2014 at 12:24

2 Answers 2

1
string.matches("[0-9]+[a-zA-Z]+[a-zA-Z0-9]*$"); 
// ensures you have atleast 1 number, 1 alphabet and starts with a number/digit
Sign up to request clarification or add additional context in comments.

7 Comments

This one works fine.I need to take the String with , as separator like this '2012IND00013,2012IND0014'.Then how to change this reg exp.Please suggest.
@Ganesh Split on commas and test each one, particularly if there are an arbitrary number of CSVs. And take some time to learn regexes, espeically when they're this simple.
@Ganesh - I would take dave's advice :)
@DaveNewton Need to check without splitting the String
@Ganesh ... So use the same expression twice, with a comma in the middle, and watch out for greediness. But a requirement to check without splitting doesn't seem reasonable. Also, not updating the question to be clearer is irresponsible--you were asked to do so, and you saw the confusion your question caused.
|
0

So you want at least one letter after the first digit. Right?

Then use

^[0-9]+[a-zA-Z][a-zA-Z0-9]*$

5 Comments

Not sure it states a letter must appear after a digit.
@PeterLawrey: Where else would it appear?
Where does the OP state it has to appear anywhere?
@PeterLawrey: They're in the first sentence after the code sample
@TimPietzcker that sentence is talking about regex the OP doesn't want. He then says But I need to check

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.