问题描述
我需要创建一种算法,在得到string
和pattern
尊重的前提下,从字符串中提取信息。
更确切地说,我定义了一个名为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]
- 假设:可接受的
18
使得每个 Label 都被 Quotes 包围。 - 如果我要查找的 Label 值是
StringPattern
的第一部分,请返回StringPattern
- 如果我要查找的 Label 值是
string.substring(0,string.indexOf(pattern.secondComponent()))
的第二个组成部分,请返回StringPattern
- 如果标签位于
string.substring(string.lastIndexOf(pattern.secondButLastComponent(),string.length()));
的中间,请执行以下操作:
我意识到此算法是错误的。在提供的示例中,当查找 Label string.indexOf(pattern.component(indexOfLabel+1))
的值时,算法正确返回子字符串[season]
和 s
之间的值。但是,当查找 Label e
的值时,该算法会将单词[episode]
中包含的e
标识为“先前组件”,从而返回Rebels
作为假定的“值”。
我起初以为利用ls
中 Label 的相对位置为优势。伪代码如下:
- 如果标签(我现在称为
StringPattern
)位于searchedLabel
的中间,请执行以下操作:- 在
pattern
处设置变量indexOfPrevIoUsQuoteInString
和indexOfCurrentLabel
; - 对于我们感兴趣的
-1
中的每个标签: - 现在变量
string
在第一个indexOfPrevIoUsQuoteInString
前的 Label 之前存储 Quote 的索引
- 在{{1}的第一个 Label 之后的 Quote 的第一个出现位置(索引
indexOfPrevIoUsQuoteInString
之后)indexOfPrevIoUsQuoteInString
中进行搜索}},并将其存储在searchedLabel
- 现在,变量
string
应该在引号indexOfPrevIoUsQuoteInString
的第一个 Label 之后存储引号的起始索引。
- 在
searchedLabel
之前的 Quote 中第一次出现(在索引indexOfPrevIoUsQuoteInString
之后)indexOfPrevIoUsQuoteInString
中搜索,并将其存储在{ {1}} - 在
searchedLabel
之后的行情的第一个出现的位置string
中搜索{em} ,并将其存储在indexOfPrevIoUsQuoteInString
-
searchedLabel
的值是索引[indexOfQuoteBeforeThisLabel
,string
)之间的子项indexOfQuoteBeforeThisLabel
。
- 在
我意识到它不起作用。要查看它,请在字符串和模式中交换季节和情节部分的位置。现在,字符串为searchedLabel
,它遵循的模式为indexOfQuoteAfterThisLabel
。由于searchedLabel
是string
中的第一个 Label (但不是第一个组成部分),因此我没有以前的 Label 可以依靠:将返回indexOfQuoteBeforeThisLabel+QuoteBeforeThisLabel.length()
和indexOfQuoteAfterThisLabel
之间的第一个子字符串,即Star Wars Rebels e18s02 - The Forgotten Droid.mp4
。另外,它非常复杂。
现在我寻求帮助。我很确定有一个巧妙的技巧可以帮助我用几行易于理解的代码来解决这个问题(个人经验...),但是我没有看到。有什么帮助吗?新观点?提示?
感谢所有帮助!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)