这是输入字符串:
#video:firstVideo.mp4#
#video:secondVideo.mp4 title:Title of second Video#
#video:thirdVideo.mp4#
我想匹配以上所有行(title: 属性是可选)。但是,下面的 Java RegEx 将仅匹配第二行。
(?<=#)video:(.*?).mp4\stitle:(.*?)(?=#)
如何更正此正则表达式以使 title: 属性可选 并返回所有三行?我会投票给任何有可行/高效答案的人!
最佳答案
试试这个:
(?<=#)video:(.*?).mp4(\stitle:(.*?))?(?=#)
或者只是:
#video:(.*?).mp4(?:\stitle:.*?)?#
参见rubular .
这取决于您希望在匹配组中包含哪些内容。
关于java - 正则表达式匹配帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7326308/