File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Generate a random value from a regex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generate a random value from a regex" Watch "Generate a random value from a regex" New topic
Author

Generate a random value from a regex

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Hello,

How can I generate a randomly word which lays in the domain of the regex "[a-zA-Z]+" ?

For example:



randomWord can be "aa" or aZd" and so on.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40


Regular expressions are used to find/match and extract stuff. It doesn't have anything that generates (random or otherwise) in the API.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

In other words, you'd have to write it. Fortunately it's really easy, at least for the regex you provide.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
okay I will try it to code.


I googled a lot and found that:

http://stackoverflow.com/questions/205411/random-string-that-matches-a-regexp

can I use perl within java? If so, then I can use something like this (this solution is from the link above):





or do I have to use somethink like this solution (the solution is from the link http://stackoverflow.com/questions/54991/generating-random-passwords):

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Nimo wrote:can I use perl within java?
Er, no.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

will you need to dynamically generated the output based on user supplied regex input? or will it ALWAYS be a-zA-Z?

Basically, you need to determine what the range of values is. a-z is 26 values, and then A-Z is another 26. That means you need to generate a number from 1-52. you then need to map your numbers to your output values.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
hello,


or will it ALWAYS be a-zA-Z?


yes, this is what I want.

In perl, does Java has a similar method like perls "random_regex" ? Should I use perl within java? Is that possible?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

So, you have the fixed regular expression "[a-zA-Z]+" and you want to generate a random word that matches it? That's easy: generate a random number (>= 1) of lower-case and upper-case letters and concatenate them. No need to bother with difficult things such as trying to use Perl from Java (which is not easy). Java has no built-in method like Perl's random_regex.

Or do you want to be able to do this for arbitrary regular expressions?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

since you're always doing a-zA-Z, i'd just write a method that converts the numbers 1-52 to your letters. Then, generate random numbers between 1-52 inclusive.

it shouldn't be hard at all.

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
okay will try it and post the code..
Velu Kasirajah
Greenhorn

Joined: Feb 06, 2010
Posts: 15
Did you take look at org.apache.commons.lang.RandomStringUtils class? This class has a variety of methods that may suit your needs.

For instance, the following code will return a randomized string of 10 chars.



Velu Kasirajah
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
hello Velu,

thank you!

that really fulfills my needs!! thanks!
Velu Kasirajah
Greenhorn

Joined: Feb 06, 2010
Posts: 15
Glad it helped
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generate a random value from a regex