Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
Hi,

In our application we have many fields that are validated by using a regex. When the user enter an invalid data we show an error message. What I'm looking for is a way to create an example of a valid entry based on the regex we used to validate the field.

I know that there's already libraries written in perl and in Java (xeger) that do this, I'm looking for one in .Net (2.0), or to someone nice enough to share his :)

thx
Posted
Comments
Maciej Los 16-May-12 11:10am    
Can't you use a help or tooltips, baloons?
Jean-Francois Lanouette 16-May-12 11:19am    
Well the layout is not the point, I'm just explaining the context... What I'm really looking for is a String Generator that use a Regex in input to give a random string. Like this http://search.cpan.org/dist/String-Random/lib/String/Random.pm but this is in Perl i want it in .net
Jean-Francois Lanouette 16-May-12 11:27am    
This ain't a 5 seconds question to answer.
Sergey Alexandrovich Kryukov 16-May-12 12:57pm    
Makes sense, my 5.
--SA

Is this what you want?

C#
private static string CreateRandomPassword(int passwordLength) 
{  
   string allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-";
  
char[] chars = new char[passwordLength];  Random rd = new Random();   


  for (int i = 0; i < passwordLength; i++)  
  {   
     chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];  
  }  
 
  return new string(chars); 
} 


I'm trying helping you...
Killzone
 
Share this answer
 
Comments
Jean-Francois Lanouette 16-May-12 11:52am    
I know that u really trying, but still there's nothing about generating a string from a regex. I've seen this post on forums too and it's about a guy that want to generate passwords randomly.

What im looking for is a function that takes a regex in input that gives a string that would match it in return, don't really have to be random it would be nice but it's not a must. If the answer would have been easy I wouldn't have to ask on a forum.
Killzone DeathMan 16-May-12 12:06pm    
ok ok sorry... I don't know... :(
I really try...and that's important, right?
Sergey Alexandrovich Kryukov 16-May-12 12:53pm    
No, it is not. Read this to see what is it:
http://code.google.com/p/xeger/
Please see my answer -- I think it's reasonable enough.
--SA
Killzone DeathMan 16-May-12 13:03pm    
Very good, you are hired!
Sergey Alexandrovich Kryukov 17-May-12 16:23pm    
Let me consider other offers first, thank you. :-)
--SA
Well if you already know of some libraries, why not make a script or console application that accepts the regex as an argument, calls one of the existing libraries, and spits out the random string?
 
Share this answer
 
Comments
Jean-Francois Lanouette 16-May-12 12:02pm    
Because i don't want to run java or perl on my .net server
lewax00 16-May-12 12:07pm    
I've seen a few ways to run Java using .Net, like this one: http://www.ikvm.net/
Sergey Alexandrovich Kryukov 16-May-12 12:56pm    
This is not a definitive answer. What libraries? And why doing a script at all?
OP needs a xeger clone, that's it.
--SA
lewax00 16-May-12 13:04pm    
"What libraries?" from question: "I know that there's already libraries written in perl and in Java (xeger)"

And if a clone doesn't exist what then (because as far as I can find, it doesn't)? Why not attempt to leverage existing tools? No point reinventing the wheel.
Sergey Alexandrovich Kryukov 17-May-12 16:22pm    
My point is: you are saying too trivial thing. If you advised some libraries, it could pass as an answer... :-)
--SA
I would take the code of Xeger and just translated it to "C#". It should be fairly easy.

http://code.google.com/p/xeger/[^].

All you need it:
svn checkout http://xeger.googlecode.com/svn/trunk/ xeger-read-only


Care to try?

Good luck,
—SA
 
Share this answer
 
Comments
Jean-Francois Lanouette 16-May-12 13:01pm    
Worth give a look, if no one shows up with it already done...
Jean-Francois Lanouette 16-May-12 13:01pm    
I can't imagine that no one had already something like this before ...
Sergey Alexandrovich Kryukov 17-May-12 16:24pm    
If so, why can't you find it? :-)
--SA
Jean-Francois Lanouette 23-May-12 8:55am    
Must be because i used Bing instead of Google :P kidding
Sergey Alexandrovich Kryukov 28-May-12 18:18pm    
Well, I guess this is some solution, maybe not as good as you expected, by you don't have anything better.
Will you accept this answer formally (green button)? -- thanks?
--SA
Xeger has been implemented in C# as the Fare project: https://github.com/moodmosaic/Fare.

The source is easy build and use.

Unfortunately, many of the .NET regular expression constructs are not supported by Xeger so you may have to limit the regular expressions or modify them to remove grouping constructs and replace character classes with explicit ranges (replace "\d" with "[0-9]" for example.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900