简体   繁体   English

从Java中的正则表达式列表中查找匹配项

[英]Find a match from a list of regular expression in java

I have a list of regular expressions in the form of [az]* (for example your*). 我有一个[az] *形式的正则表达式列表(例如your *)。 I need to find the possible matches for words read from a regular text file from the list of regular expressions. 我需要从正则表达式列表中找到从正则文本文件中读取的单词的可能匹配项。 I can use the Pattern class for each of the list items and then use matcher to match the word with each member of the list, but this process seems time consuming. 我可以为每个列表项使用Pattern类,然后使用匹配器将单词与列表的每个成员进行匹配,但是此过程似乎很耗时。 The regex list is big (around 2500 expressions) and the number of words I need to match is also large (in millions) so I need something fast- and it should be in Java. 正则表达式列表很大(大约2500个表达式),我需要匹配的单词数也很大(以百万计),所以我需要快速的东西-它应该是Java语言。 Is there a better way to search for a regex match for a word from a list of regexes? 有没有更好的方法来从正则表达式列表中搜索单词的正则表达式匹配项?

If all your expressions are all in the form foo* then could you not just replace all the pattern match attempts to calls to word.startsWith(pattern) ? 如果您所有的表达式都以foo*的形式表示,那么您能不能替换掉所有对word.startsWith(pattern)的模式匹配尝试? (Having first trimmed the '*' from the pattern, if is is actually included in the pattern list.) Or - if the '*' is optional - with either that for word.equals(pattern) depending on the pattern? (首先将模式中的'*'修剪word.equals(pattern) ,如果它实际上包含在模式列表中。)或-如果'*'是可选的-根据word.equals(pattern) ,是否使用word.equals(pattern)呢?

I solved the problem using a Trie tree. 我使用特里树解决了这个问题。 – Farig Sadeque –法里格·萨德克(Farig Sadeque)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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