8 Replies - 16003 Views - Last Post: 19 July 2009 - 04:12 AM Rate Topic: -----

#1 przemass   User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 166
  • Joined: 18-July 09

random string based on regular expression

Posted 18 July 2009 - 09:37 AM

Hello, I must create a function that return a random string based on a regular expression as a argument.

For example:
expression: [a-z]{3}
possible strings: sdf, rtg, ass, etc...

I've found something about my problem here (link) but this is by using perl only, I must do this in by using C#.

Can anyone help me?

Is This A Good Question/Topic? 0
  • +

Replies To: random string based on regular expression

#2 Renagado   User is offline

  • D.I.C Regular
  • member icon

Reputation: 118
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: random string based on regular expression

Posted 18 July 2009 - 11:01 AM

Well am sure we can help, but what exactly is it that you don't get?
Was This Post Helpful? 0
  • +
  • -

#3 przemass   User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 166
  • Joined: 18-July 09

Re: random string based on regular expression

Posted 18 July 2009 - 11:25 AM

Sorry, but I think you can't help me. I hope I'm wrong...

I need function like this:
public string getRandomString(Regex r)
{
	string s;

	//some code here creates s

	//s must match to r
	return s;
}


Was This Post Helpful? 0
  • +
  • -

#4 Renagado   User is offline

  • D.I.C Regular
  • member icon

Reputation: 118
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: random string based on regular expression

Posted 18 July 2009 - 11:32 AM

Well I just might be able to help if you have a clearer question to ask. Is it the c# you don't know? The algorithm?
I say put the possible characters into an array, pick randomly the number of letters you need, put it into another array, then iterate through all possibilities. Not that hard if you ask me.
Was This Post Helpful? 0
  • +
  • -

#5 przemass   User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 166
  • Joined: 18-July 09

Re: random string based on regular expression

Posted 18 July 2009 - 11:46 AM

Ok, let's say I want to get random string that match to this regex "[a-z0-9]@[a-z0-9]\.[a-z]{2,5}". If I used your solution i could generate 1000 strings and none of them could match to regex. Sorry for my English but I think you understand what I want to get.
Was This Post Helpful? 0
  • +
  • -

#6 Renagado   User is offline

  • D.I.C Regular
  • member icon

Reputation: 118
  • View blog
  • Posts: 388
  • Joined: 14-June 09

Re: random string based on regular expression

Posted 18 July 2009 - 11:58 AM

Right, but you wrote you wanted a just a random string composed a given set of characters in your first post.
Was This Post Helpful? 0
  • +
  • -

#7 janne_panne   User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: random string based on regular expression

Posted 18 July 2009 - 01:36 PM

This looked like a nice challenge so I decided to went for it and try it out. I first decided to fetch all the tokens within the pattern so I figured a way to split the example string you gave into pieces. The result was:

"[a-z0-9]@[a-z0-9]\.[a-z]{2,5}"

splitted into:

piece[0] = [a-z0-9]
piece[1] = @
piece[2] = [a-z0-9]
piece[3] = \.
piece[4] = [a-z]
piece[5] = {2,5}

This way I can quite easily generate the required characters. First check if a piece is either box braces [], curly braces {} or just regular string.

If it was regular string, I didn't do anything to it (at least not yet, maybe I'll add some logic into them later too, like . can be any character etc.).

If it is box braces, I check for patters: "a-z", "A-Z", "0-9". Then I'll just use Random class' Next() method to find out the ascii value of one of those patterns' character and translate that ascii to char. And I also check here if the box braces is followed by curly braces and if it is, find out how many letters are generated.

I can provide the code I made later, I'll give you some time to figure out the code by yourself.
But my method isn't perfect so don't count on it too much.
Was This Post Helpful? 0
  • +
  • -

#8 przemass   User is offline

  • D.I.C Head

Reputation: 30
  • View blog
  • Posts: 166
  • Joined: 18-July 09

Re: random string based on regular expression

Posted 19 July 2009 - 03:02 AM

Ok, what about this regex "([asdf].{5}([[email protected]#$]+))+". I try to say that exist a lot of possibilities. I rather need some library to do that...
Was This Post Helpful? 0
  • +
  • -

#9 janne_panne   User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: random string based on regular expression

Posted 19 July 2009 - 04:12 AM

You can either start googling for a library or start writing your own.

I don't think it's that hard to write a decent method for your needs because when generating random string lots of regex rules do not apply. For example you can't use * or + to indicate repeating. You just check what's inside box braces [] and generate one of those letters as many times as it says in curly braces {}. And if there is dot, you generate any letter as many times as it says in curly braces.

This is a string generated with my method, given pattern is the one in your message ("([asdf].{5}([[email protected]#$]+))+"):

(f:$^X(@+))+

I'm not doing anything to normal braces () or to plus signs +:
f = result of [asdf]
:$^X = result of .{5}
@ = result of [[email protected]#$]

And results for another pattern ("[a-z0-9A-Z]{3}@[a-z0-9]\.[a-zA-Z]{2,5}"):

[email protected]

e32 = result of [a-z0-9A-Z]{3}
@ = nothing is done
6 = result of [a-z0-9]
. = result of \. (because of escape sequence '\')
oK = result of [a-zA-Z]{2,5}


And a good thing in your own library is that you can add features when you notice you need them. So from now on I'll keep improving this Random string generator whenever I notice it's missing something. One day it might be almost perfect for its purpose.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1