【问题标题】:Java Scan for a keywords following wordJava 扫描单词后面的关键字
【发布时间】:2018-03-16 20:36:48
【问题描述】:

我要解决一个非常具体的问题:

  • 使用 java 和 eclipse
  • 在长字符串中包含大量字符(单词、括号、问号等)
  • 尝试按关键字对传入的信息进行排序以取得进一步进展

所以我尝试的是:

  1. 获取传入的字符串,就像 “我们有一个新成员。他的名字是:Peter。他很漂亮。他是该组的成员:devceloper。他的生日是:13.08.2001 . 我们还有一个新成员...
  2. 扫描字符串中的一些关键字,这些关键字将按照特定顺序排列,例如“姓名”、“组”和“生日”
  3. 识别关键字,后面的“不需要”字符(每次都一样)
  4. 提取相关信息,放入二维数组中。 所以我的输出应该看起来像 {{"peter", "developer", "13.08.2001"}, {"susan", "marketing", "02.03.1997"}...}

为了完成这个,我找到了一个基本脚本,它应该“提取”单独的单词,但它仍然有问题,所以它并没有真正的帮助。

int indexOfSpace = 0; 
int nextIndexOfSpace = 0;

String sentence = "This is a sentence";

int lastIndexOfSpace = sentence.lastIndexOf(" "); 
while(indexOfSpace != lastIndexOfSpace) { 
    nextIndexOfSpace = sentence.indexOf(" ",indexOfSpace);
    String word = sentence.substring(indexOfSpace,nextIndexOfSpace);
    System.out.println("Word: " + word + " Length: " + word.length());
    indexOfSpace = nextIndexOfSpace; }

String lastWord = sentence.substring(lastIndexOfSpace);
System.out.println("Word: " + lastWord + " Length: " + lastWord.length());

我不希望你给我现成的解决方案,但我可能需要一些编程步骤的提示;)

【问题讨论】:

  • 如果你想要“一些提示”,那么 SO 不适合你。这是一个纯粹的问答论坛。

标签: java string input keyword


【解决方案1】:

你可以使用类似于这个的正则表达式:

name[^:]*:\s*(\w+).*?group[^:]*:\s*(\w+).*?birthday[^:]*:\s*(\d+\.\d+\.\d+)

对于输入字符串:

我们有一个新成员。他的名字是:彼得。他很不错。他是该组的成员:devceloper。他的生日也是:13.08.2001。我们还有一个新成员...

它将捕获以下组:

  • 彼得
  • 开发者
  • 13.08.2001

对模式使用匹配器,您可以迭代所有匹配项。

示例代码:

String input = "we got a new member. he's name is: Peter. He is pretty nice. he "
            + "is a member of the group: devceloper. Also he's birthday is: 13.08.2001."
            + " As well we got a new member she's name is: Sara. She is pretty nice. "
            + "she is a member of the group: customer. Also her birthday is: 21.01.1998";

Pattern pattern = Pattern.compile("name[^:]*:\\s*(\\w+).*?group[^:]*:\\s*(\\w+).*?birthday[^:]*:\\s*(\\d+\\.\\d+\\.\\d+)");

Matcher matcher = pattern.matcher(input);

while(matcher.find()) {
    System.out.printf("Match found. name: %s, group: %s, birthday: %s %n", matcher.group(1), matcher.group(2), matcher.group(3));
}

输出:

Match found. name: Peter, group: devceloper, birthday: 13.08.2001 
Match found. name: Sara, group: customer, birthday: 21.01.1998 

【讨论】:

  • 但是在同一个字符串中有几个信息“银行”。当文本中提到大约 10 个人时,我如何跟踪我停止的位置,这些人保存在字符串中?我需要提取另一个信息,然后在最后一个停止点继续“读取”字符串
  • 匹配器负责匹配输入字符串以获得更多正则表达式匹配
  • 哇 ... 非常糟糕 :) 早些时候我会尝试扫描字符串中的字符并尝试在新字符串中逐字母写入并将此字符串写入数组。这样就容易多了……
  • 所以我尝试将代码应用到我的项目中。我的输入现在看起来像:"name":"peter", "group":" developer", "age":"17" .... (some bunch of other information) ... "name":"Sara", "group" ...` 我的代码片段看起来像:Pattern pattern = Pattern.compile(".*name\":\"(\\w+).*group\":\"(\\w+).*age\":\"(\\w+).*"); Matcher matcher = pattern.matcher(input); while (matcher.find()) { System.out.printf("name: %s, group %s, age %s %n", matcher.group(1), matcher.group(2), matcher.group(3)); } 但输出只给了我最后一个人的信息并停止:/
  • @renegade2k 提防贪婪的.*,使用惰性版本.*? 否则他们会消耗整个剩余的字符串
【解决方案2】:

如果您确定字符串将始终遵循相同的形式,则可以实现正则表达式匹配。这个想法是使用组来捕获您感兴趣的子字符串。

例如,您可以使用.*name is: (\w+) 从字符串中捕获Peter。同样,您可以将其应用于其他令牌。

【讨论】:

  • 字符串总是重复的并且遵循相同的结构。唯一的变化是缺少一些信息(有点“生日:[]”),所以我需要用空格或 null 或类似的东西来填补空白,因为我将使用数组来显示表格之后
猜你喜欢
  • 1970-01-01
  • 2020-12-14
  • 1970-01-01
  • 1970-01-01
  • 2019-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多