正则表达式匹配不完整的表达式
我想编写正则表达式来识别电视剧集;我正在用 Java 做这个。标题是这样写的:
Title 2x05
其中 2 是季节,5 是剧集;所以我使用了这个表达式:
\d*x\d*
它工作得很好,除非标题包含一个或多个“x”字符;在这种情况下,我与这个角色完全匹配,导致了明显的问题。有什么办法可以避免这种情况吗?
I'd like to write the regular expression to recognize the series tv episode; I'm doing this in Java. Titles are written this way:
Title 2x05
Where 2 is the season and 5 is the episode; so I used this expression:
\d*x\d*
And it works perfectly fine, except when the title includes one or more "x" character; in this case I have a match exactly on this character, causing obvious problems. Is there any way to avoid this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
另一种解决方案,特别是如果您确实想要匹配
x
周围的零个或多个数字:使用 单词边界。将匹配
2x5
、x3
、25x
甚至x
,但不会匹配x
在text
等中。Another solution, especially if you actually do want to match zero or more digits around
x
: Use word boundaries.will match
2x5
,x3
,25x
or evenx
, but it won't match thex
intext
etc.您是否尝试在季节和剧集之间匹配任意数量的
x
?如果是,请尝试使用
+
而不是*
以确保季和剧集至少有一位数字,并且中间至少有一个x
。Are you trying to match any number of
x
in between season and episode? If yes, tryuse
+
instead of*
to make sure there is at least one digit for season and episode and at lease onex
in between.尝试使用此表达式代替“
\d+x\d+
”。请注意,
+
字符将匹配前面的标记(数字)的一个或多个,而*
将匹配零个或多个前面的标记的。Try this expression instead "
\d+x\d+
".Note that the
+
character will match one or more of the preceding token (a digit) whereas the*
will match zero or more of the preceding token.您甚至可以使用组同时检索季节和剧集信息:
结果:第 2 季,第 8 集
You can even use groups to simultaneously retrieve the season and episode informations :
Result : Season 2, Episode 8
试试这个:
\d+x\d+
+
: 1 或更多*
: 0 或更多?
: 1或根本没有但请记住,这不是针对您的情况进行测试的最可靠的方法。如果有人输入 99x9999999,那将是第 99 季和 9999999 集。如果您想限制可能的季节和剧集数量,请检查此处。
Try this:
\d+x\d+
+
: 1 or more*
: 0 or more?
: 1 or not at allBut remember that this is not the most robust method to test in your case. If someone enters 99x9999999, that will be 99th season and 9999999 episode. If you want to limit the number of possible seasons and episodes, check here.
这个怎么样?
假设剧集不超过 99 季,每季不超过 999 集。
How about this?
Assuming there are not more than 99 seasons and more than 999 episodes per season.