Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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
Spring VI Editor Clojure
  • 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
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!