public class RandomStringGenerator extends Object
Constructor and Description |
---|
RandomStringGenerator()
Instantiate generator with default number of upper limit for regex
quantifiers, for example
* , + and etc (default value: 10). |
RandomStringGenerator(int numOfUpperLimit)
Instantiate generator with number of upper limit for regex quantifiers,
for example
* , + and etc. |
Modifier and Type | Method and Description |
---|---|
String |
generateByRegex(String regex)
Generate random string from regular expression.
|
String |
generateFromPattern(String pattern)
Generate random string from pattern.
|
int |
getNumOfUpperLimit()
Get number of upper limit for regex quantifiers,
for example
* , + and etc. |
void |
setNumOfUpperLimit(int numOfUpperLimit)
Set number of upper limit for regex quantifiers,
for example
* , + and etc. |
public RandomStringGenerator(int numOfUpperLimit)
*
, +
and etc.numOfUpperLimit
- Number of upper limit for quantifierspublic RandomStringGenerator()
*
, +
and etc (default value: 10).public String generateFromPattern(String pattern)
You can use following characters as pattern.
c
: Any Latin lower-case characterC
: Any Latin upper-case charactern
: Any digit [0-9]
!
: A symbol character [~`!@$%^&*()-_+= []|\:;"'.<>?/#,]
.
: Any of the aboves
: A "salt" character [A-Za-z0-9./]
b
: An ASCIII character which has code from 0 to 255e.g.
RandomStringGenerator generator = new RandomStringGenerator();
// generates random string (e.g. "aB4@X.Ç")
String randomString = generator.generateFromPattern("cCn!.sb");
pattern
- Pattern stringpublic String generateByRegex(String regex)
You can use following meta characters as regex.
\w
: Alphanumeric + "_" [A-Za-z0-9_]
\d
: Digits [0-9]
\W
: Printable characters other than those in \w\D
: Printable characters other than those in \d\s
: Whitespace characters [ \t]
\S
: Printable characters.
: Printable characters[]
: Character classes (Example of usage [a-zA-Z]
){}
: Repetition*
: Same as {0,}?
: Same as {0,1}+
: Same as {1,}e.g.
RandomStringGenerator generator = new RandomStringGenerator();
// generates random string (e.g. "a5B123 18X")
String randomString = generator.generateByRegex("\\w+\\d\*\s[0-9]{0,3}X");
regex
- Pattern based on regular expressionpublic int getNumOfUpperLimit()
*
, +
and etc.public void setNumOfUpperLimit(int numOfUpperLimit)
*
, +
and etc.numOfUpperLimit
- Number of upper limit for quantifiersCopyright © 2015. All rights reserved.