• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert a String into a Regular Expression

 
Alex Loaiza
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, anyone knows if there is a way to convert an input string into a Regular Expression??

As a example, if I input the string: "email@mail.com" the output string should be like: "[A-Za-z]+@[A-Za-z]+\\.com"

Thanks in advance!!
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that's a well-defined question. There are many
regular expressions that could possibly match a given string, right?
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, anyone knows if there is a way to convert an input string into a Regular Expression??

There's no generic way to do that. There are innumerable regexes that would match that string.
 
Alex Loaiza
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, in fact, what I need is: given a certain String verify if it is an alfanumeric string and covert it into a Reggex pattern like this [0-9\w], or if it is a numeric (numbers only) string convert it to [0-9]...

...I don't need a too complex solution, Thanks!!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, if you had a bunch of strings, you can imagine a tool that would create the simplest possible regexp that matched them all. I'm sure such a thing has been done by someone... and indeed, I Googled for "regexp generator" and "regular expression generator" (both in quotes) and both returned many promising hits.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Alex Loaiza:
Well, in fact, what I need is: given a certain String verify if it is an alfanumeric string and covert it into a Reggex pattern like this [0-9\w], or if it is a numeric (numbers only) string convert it to [0-9]...

Then why didn't your example convert the "com" at the end?
 
Alex Loaiza
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albrechtsen:

Then why didn't your example convert the "com" at the end?


Well, maybe I forget that ".com" at the end, but I think that you already understad what I meant. A simple reggex generator that matches in a "generic" way any input string given...

thanks!
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something simple-minded like this:

[ November 18, 2005: Message edited by: Jeff Albrechtsen ]
 
Alex Loaiza
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's correct, just that simple!!

Thanks a lot!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic