0

Java code to generate random string based on regex. I tried generex library , but it's not giving exact output. For example:

       Generex generex = new Generex("^[6-9]\\d{9}$");
       System.out.println(generex.random());

Output is : ^8677547981$ while i was expecting output to be 8677547981.It seems generex is not able to handle special characters like "^" , "$" etc.

Can someone pls help?

1
  • Try escaping the dollar sign: \\$ Commented Feb 8, 2016 at 11:50

2 Answers 2

6

Generex doesn't need ^ and $. The content created by it matches the regex fully, instead of a partial match.

Generex generex = new Generex("[6-9]\\d{9}");
System.out.println(generex.random());
Sign up to request clarification or add additional context in comments.

2 Comments

The point is not that it generates the full string, but that ^ and $ are valid regex characters but it is treating them like literals. It should handle a valid regex without modification without introducing extraneous characters that would cause subsequent regex validation to fail.
@JeffVincent You should register that bug at the Generex project, if there isn't a duplicate already, I'm not affiliated with the project
2

Generex uses dk.brics.automaton.RegExp class. It supports another set of regular expression operations as java.util.regex.Pattern. See RegExp API

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.