aspose file tools*
The moose likes Beginning Java and the fly likes Convert a String into a Regular Expression Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert a String into a Regular Expression" Watch "Convert a String into a Regular Expression" New topic
Author

Convert a String into a Regular Expression

Alex Loaiza
Greenhorn

Joined: Jul 22, 2005
Posts: 12
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

Joined: Sep 16, 2005
Posts: 1780
I don't think that's a well-defined question. There are many
regular expressions that could possibly match a given string, right?


There is no emoticon for what I am feeling!
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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

Joined: Jul 22, 2005
Posts: 12
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Jul 22, 2005
Posts: 12
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

Joined: Sep 16, 2005
Posts: 1780
Something simple-minded like this:

[ November 18, 2005: Message edited by: Jeff Albrechtsen ]
Alex Loaiza
Greenhorn

Joined: Jul 22, 2005
Posts: 12
That's correct, just that simple!!

Thanks a lot!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert a String into a Regular Expression