字符串分析算法的更正

问题描述

我需要创建一种算法,在得到stringpattern尊重的前提下,从字符串中提取信息。

更确切地说,我定义了一个名为StringPattern的对象,该对象由以下三个元素组成:

  • 标签,即String,以[开头,以]结尾(例如:[nameShow],{{1} },[season]);
  • 行情,或固定的[episode] s;
  • 自由字符串,或任意长度的String包括空格{,由字符String表示。

我要提取内容*标签的值。例如:字符串string遵循模式Star Wars Rebels s02e18 - The Forgotten Droid.mp4标签*s[season]e[episode] *.mp4的值为[season],而02的值为[episode]


我的第一个解决方案如下:

  1. 假设:可接受的18使得每个 Label 都被 Quotes 包围。
  2. 如果我要查找的 Label 值是StringPattern的第一部分,请返回StringPattern
  3. 如果我要查找的 Label 值是string.substring(0,string.indexOf(pattern.secondComponent()))的第二个组成部分,请返回StringPattern
  4. 如果标签位于string.substring(string.lastIndexOf(pattern.secondButLastComponent(),string.length()));的中间,请执行以下操作:
    1. 计算pattern的各个组成部分中 Label 的索引,并将其保存在变量pattern
    2. 如果标签不在indexOfLabel的组成部分中,请返回pattern
    3. 否则,返回上一个组件的末尾(即null)和下一个组件的开始(即string.indexOf(pattern.component(indexOfLabel-1)) + pattern.component(indexOfLabel-1).length())之间的子字符串

我意识到此算法是错误的。在提供的示例中,当查找 Label string.indexOf(pattern.component(indexOfLabel+1))的值时,算法正确返回子字符串[season] s间的值。但是,当查找 Label e的值时,该算法会将单词[episode]中包含的e标识为“先前组件”,从而返回Rebels作为假定的“值”。


我起初以为利用ls Label 的相对位置为优势。伪代码如下:

  1. 如果标签(我现在称为StringPattern)位于searchedLabel的中间,请执行以下操作:
    • pattern处设置变量indexOfPrevIoUsQuoteInStringindexOfCurrentLabel
    • 对于我们感兴趣的-1中的每个标签
      1. StringPattern搜索出现在当前 Label 之前的 Quote 的第一个出现位置,并将该值保存在{ {1}}
    • 现在变量string在第一个indexOfPrevIoUsQuoteInString
    • 前的 Label 之前存储 Quote 的索引
    • 在{{1}的第一个 Label 之后的 Quote 的第一个出现位置(索引indexOfPrevIoUsQuoteInString之后)indexOfPrevIoUsQuoteInString中进行搜索}},并将其存储在searchedLabel
    • 现在,变量string应该在引号indexOfPrevIoUsQuoteInString
    • 的第一个 Label 之后存储引号的起始索引。
    • searchedLabel之前的 Quote 中第一次出现(在索引indexOfPrevIoUsQuoteInString之后)indexOfPrevIoUsQuoteInString搜索,并将其存储在{ {1}}
    • searchedLabel之后的行情的第一个出现的位置string搜索{em} ,并将其存储在indexOfPrevIoUsQuoteInString
    • searchedLabel的值是索引[indexOfQuoteBeforeThisLabelstring)之间的子项indexOfQuoteBeforeThisLabel

我意识到它不起作用。要查看它,请在字符串和模式中交换季节和情节部分的位置。现在,字符串为searchedLabel,它遵循的模式为indexOfQuoteAfterThisLabel。由于searchedLabelstring中的第一个 Label (但不是第一个组成部分),因此我没有以前的 Label 可以依靠:将返回indexOfQuoteBeforeThisLabel+QuoteBeforeThisLabel.length()indexOfQuoteAfterThisLabel间的一个子字符串,即Star Wars Rebels e18s02 - The Forgotten Droid.mp4。另外,它非常复杂。

现在我寻求帮助。我很确定有一个巧妙的技巧可以帮助我用几行易于理解的代码解决这个问题(个人经验...),但是我没有看到。有什么帮助吗?新观点?提示

感谢所有帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)