This question already has an answer here:
i want to generate random String who have this form
[A-Za-z0-9]{5,10}
I don't have any idea how to do it, i should use regular expressions or random function ?
This question already has an answer here:
i want to generate random String who have this form
[A-Za-z0-9]{5,10}
I don't have any idea how to do it, i should use regular expressions or random function ?
Its not possible to generate random String using regular expression. Regular expression is way to specify and recognize Patterns of texts, characters and words. You can use following code to generate random string.
I'd stick to a Java-solution in this case, something along the lines of:
You can call this with
getRandomValue(5, 10);
I have not tried this code, since I have no IDE available
Note, if you're not apposed to using third party libraries, there are numerous available.
You could use RandomStringUtils.randomAlphanumeric(int count) found in Apache's commons-lang library and specify a different count argument each time (from 5 to 10)
http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/RandomStringUtils.html#randomAlphanumeric%28int%29
You can't use regexes (in Java) to generate things.
You need to use a "random" number generator (e.g. the
Random
class) to generate a random number (between 5 and 10) of random characters (in the set specified.) In fact, Java provides more than one generator ... depending on how important randomness is. (TheRandom
class uses a simple pseudo-random number generator algorithm, and the numbers it produces are rather predictable ...)I suspect this is a "learning exercise" so I won't provide code. (And if it is not a learning exercise, you should be capable of writing it yourself anyway ...)