简体   繁体   中英

Java library for generating regular expression from input string

There are many websites which take a string as user input and allow you to create a regular expression (regex) from pieces of the string.

But I could not find any java library which does the same. Is there any Java library available which generates a regular expression that exactly matches a string?

String inputString = "ABC345";
String regularExpression = Something.generateRegEx(inputString);

or something like that.

Note: I have a condition wherein I want to take some string from user, generate regular expression and then match for that pattern on some data-sets to extract similar patterns. I have created a small utility, but it is not that reliable yet. Moreover, I am looking for some well-tested library.

EDIT :

Please visit txt2re.com . I want a java library which performs the same function.

Pattern.quote(String)返回与指定字符串完全匹配的(字符串)正则表达式。

I think, the txt2re.com has a database from known regular expressions, since the tool extends its answers with semantics like "date" or "email" for date and email formats. Otherwise, it gives an expression, which validates only a string but not a "regular language". Regular languages are expressed by regular expressions and they can be calculated by finite-state machines, but they are sets of limited words (all finite languages are regular). For example a simple language like:

L = { (a^n)(b^n) | n >= 0 } is not regular. (proof with pumping lemma)

L = {ab, aabb, aaabbb,...} (not- regular) 

if you consider, that the input is a set of infinite words (inclusive natural languages), however, the regular expressions can not describe all of them. In order to generate regular expressions for a language, you had to first describe it with a (TYPE-3) grammar.

if your language has only a word like this:

L = { your.name@example.com }

then you can write a basic compiler iterating over the chars while checking their types, pseudo:

s = size(input) 
result = ""
for (i = 0; i < s; i++) {
   if input[i] is numeric
      result += "d"
   else if input[i] is word
      result += "w" 
   ...
}
return result

基于遗传算法的java库如regex ++ url: https//github.com/MaLeLabTs/RegexGenerator可用于相同的目的。

If what you want is to find a regex matching a given String, this does not make sense because there exists an infinite number of it.

On a contrary if you want to build a Pattern object from a regex that is input from the user, use the standard java API ( java.util.regex.* ) this way :

Pattern p = Pattern.compile(inputString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM