gpt4 book ai didi

java - 找出字符串部分是否是潜在正则表达式匹配的最佳方法

转载 作者:行者123 更新时间:2023-11-30 05:06:27 25 4
gpt4 key购买 nike

你会怎么做:

我有一个字符串和一些正则表达式。然后我迭代该字符串,在每次迭代中我需要知道该字符串的部分(字符串索引 0 到当前迭代的字符串索引)是否可能在下一次迭代中完全匹配一个或多个给定的正则表达式。

感谢您的帮助。

最佳答案

像这样的代码怎么样:

// all of *greedy* regexs into a list
List<String> regex = new ArrayList<String>();
// here is my text
String mytext = "...";

String tmp = null;
// iterate over letters of my text
for (int i = 0; i < mytext.length(); i++) {
// substring from 0. position till i. index
tmp = mytext.substring(0, i);
// append regex on sub text
for (String reg : regex ) {
Pattern p = Pattern.compile(reg);
Matcher m = p.matcher(tmp);
// if found, do smt
if (m.find() ) { bingo.. do smt! }
}
}

关于java - 找出字符串部分是否是潜在正则表达式匹配的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4954797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com