• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Generate a random value from a regex  RSS feed

 
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.
 
author
Sheriff
Posts: 23603
138
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
 
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.
 
lowercase baba
Bartender
Posts: 12628
50
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?
 
Java Cowboy
Sheriff
Posts: 16084
88
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
Posts: 12628
50
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..
 
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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!