• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generate a random value from a regex

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21015
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Henry
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nimo wrote:can I use perl within java?
Er, no.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay will try it and post the code..
 
Velu Kasirajah
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Velu,

thank you!

that really fulfills my needs!! thanks!
 
Velu Kasirajah
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad it helped
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic