Java正则表达式尝试分割字符串

标签 java regex pattern-matching

嗨,我正在尝试分割这个字符串(它很长):

Library Catalogue Log off | Borrower record | Course Reading | Collections | A-Z E-Journal list | ILL Request | Help   Browse | Search | Results List | Previous Searches | My e-Shelf | Self-Issue | Feedback       Selected records:  View Selected  |  Save/Mail  |  Create Subset  |  Add to My e-Shelf  |        Whole set:  Select All  |  Deselect  |  Rank  |  Refine  |  Filter   Records 1 - 15 of 101005 (maximum display and sort is 2500 records)         1 Drower, E. S. (Ethel Stefana), Lady, b. 1879. Lady E.S. Drower’s scholarly correspondence : an intrepid English autodidact in Iraq / edited by 2012. BK Book University Library( 1/ 0) 2 Kowalski, Robin M. Cyberbullying : bullying in the digital age / Robin M. Kowalski, Susan P. Limber, Patricia W. Ag 2012. BK Book University Library( 1/ 0) ...  15 Ambrose, Gavin. Approach and language [electronic resource] / Gavin Ambrose, Nigel Aono-Billson. 2011. BK Book

这样我要么回来:

1 Drower, E. S. (Ethel Stefana), Lady, b. 1879. Lady E.S. Drower’s scholarly correspondence : an intrepid English autodidact in Iraq / edited by 2012. BK Book University Library( 1/ 0)

// Or

1 Drower, E. S. (Ethel Stefana), Lady, b. 1879. Lady E.S. Drower’s scholarly correspondence : an intrepid English autodidact in Iraq 

这只是一个示例,1 Drower,E. S. ...不会是静态的。虽然每次输入都会不同(1 和 2 之间的细节),但字符串的总体布局始终是相同的。

我有:

String top = ".*         (.*)";
String bottom = "\( \d/ \d\)\W*";
Pattern p = Pattern.compile(top); //+bottom
Matcher matcher = p.matcher(td); //td is the input String
String items = matcher.group();
System.out.println(items);

当我使用 top 运行它时,它会删除所有 header ,但我得到的只是未找到匹配项bottom 是我尝试分割字符串的其余部分。

如果需要,我可以发布最多 15 条的所有输入。我需要的是拆分输入字符串,以便我可以处理 15 个结果中的每个结果。

感谢您的帮助!

最佳答案

这将为您提供两种输入。这是你想要的吗?

String text = "Library Catalogue Log off ..."; \\truncated text

Pattern p = Pattern.compile("((1 Drower.+Iraq).+0\\)).+2 Kowalski");
Matcher m = p.matcher(text);
if (m.find()) {
    System.out.println(m.group(1));
    System.out.println(m.group(2));
}

Compile and run code here.

关于Java正则表达式尝试分割字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9708914/

相关文章:

javascript - JavaScript中的分词器和匹配器

f# - 为什么一个变量可以在一个模式中绑定(bind)两次?

scala - 为什么 scala 编译器有时会在 "pattern matching"上发出警告,有时却不会?

c# - SAX 与 XmlTextReader - C# 中的 SAX

python - 正则表达式查找单词中的连续字符并删除该单词

仅第一个字符字母表的 JavaScript 正则表达式,第二个及以后可以是字母数字或特殊字符(连字符、逗号和空格)

list - 在列表中找到倒数第二个项目,请解释此解决方案

Java如何查找方法被调用的次数

java - 即使 containsKey() 返回 true,从 HashMap 检索时也会出现空指针异常

Java Eclipse - 如何更改类路径?