gpt4 book ai didi

java - 正则表达式通配符匹配分割与java split方法

转载 作者:太空宇宙 更新时间:2023-11-04 11:25:29 25 4
gpt4 key购买 nike

我知道之前也有类似的问题,但我想做一个自定义操作,但我不知道如何去做。我想用正则表达式分割一串数据,例如,但这次我知道起始字符和结束字符,例如:

String myString="Google is a great search engine<as:...s>";

是开始和结束字符...是动态的,我无法预测它的值(value)

我希望能够从开头 分割字符串其中包含动态字符串。

喜欢:

myString.split("<as:/*s>");

类似这样的事情。我还想获取字符串中所有出现的 。我知道这可以用正则表达式来完成,但我以前从未这样做过。我需要一种简单而简洁的方法来做到这一点。提前致谢

最佳答案

而不是使用 .split() ,我只是使用 Pattern 进行提取和Matcher 。这种方法可以找到 <as: 之间的所有内容。和s>并将其提取到捕获组。第 1 组包含您想要的文本。

public static void main(String[] args)
{
final String myString="Google is a great search engine<as:Some stuff heres>";

Pattern pat = Pattern.compile("^[^<]+<as:(.*)s>$");

Matcher m = pat.matcher(myString);
if (m.matches()) {
System.out.println(m.group(1));
}
}

输出:

Some stuff here

如果您需要开头的文本,也可以将其放入捕获组中。

编辑:如果有多个<as...s>在输入中,那么以下将收集所有这些。编辑2:增加逻辑。添加了对空性的检查。

public static List<String> multiEntry(final String myString)
{
String[] parts = myString.split("<as:");

List<String> col = new ArrayList<>();
if (! parts[0].trim().isEmpty()) {
col.add(parts[0]);
}

Pattern pat = Pattern.compile("^(.*?)s>(.*)?");
for (int i = 1; i < parts.length; ++i) {
Matcher m = pat.matcher(parts[i]);
if (m.matches()) {
for (int j = 1; j <= m.groupCount(); ++j) {
String s = m.group(j).trim();
if (! s.isEmpty()) {
col.add(s);
}
}
}
}

return col;
}

输出:

[Google is a great search engine, Some stuff heress, Here is Facebook, More Stuff, Something else at the end]

编辑 3:此方法使用查找和循环来进行解析。它也使用可选的捕获组。

public static void looping()
{
final String myString="Google is a great search engine"
+ "<as:Some stuff heresss>Here is Facebook<as:More Stuffs>"
+ "Something else at the end" +
"<as:Stuffs>" +
"<as:Yet More Stuffs>";

Pattern pat = Pattern.compile("([^<]+)?(<as:(.*?)s>)?");

Matcher m = pat.matcher(myString);
List<String> col = new ArrayList<>();

while (m.find()) {
String prefix = m.group(1);
String contents = m.group(3);

if (prefix != null) { col.add(prefix); }
if (contents != null) { col.add(contents); }
}

System.out.println(col);
}

输出:

[Google is a great search engine, Some stuff heress, Here is Facebook, More Stuff, Something else at the end, Stuff, Yet More Stuff]

其他编辑:编写了一些快速测试用例(使用 super 黑客帮助类)来帮助验证。这些都通过了(更新)multiEntry :

public static void main(String[] args)
{
Input[] inputs = {
new Input("Google is a great search engine<as:Some stuff heres>", 2),
new Input("Google is a great search engine"
+ "<as:Some stuff heresss>Here is Facebook<as:More Stuffs>"
+ "Something else at the end" +
"<as:Stuffs>" +
"<as:Yet More Stuffs>" +
"ending", 8),
new Input("Google is a great search engine"
+ "<as:Some stuff heresss>Here is Facebook<as:More Stuffs>"
+ "Something else at the end" +
"<as:Stuffs>" +
"<as:Yet More Stuffs>", 7),
new Input("No as here", 1),
new Input("Here is angle < input", 1),
new Input("Angle < plus <as:Stuff in as:s><as:Other stuff in as:s>", 3),
new Input("Angle < plus <as:Stuff in as:s><as:Other stuff in as:s>blah", 4),
new Input("<as:To start with anglass>Some ending", 2),
};


List<String> res;
for (Input inp : inputs) {
res = multiEntry(inp.inp);
if (res.size() != inp.cnt) {
System.err.println("FAIL: " + res.size()
+ " did not match exp of " + inp.cnt
+ " on " + inp.inp);
System.err.println(res);
continue;
}
System.out.println(res);
}
}

关于java - 正则表达式通配符匹配分割与java split方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44424452/

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