正则表达式匹配不完整的表达式

发布于 2024-12-20 05:42:32 字数 228 浏览 1 评论 0原文

我想编写正则表达式来识别电视剧集;我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浮光之海 2024-12-27 05:42:32

另一种解决方案,特别是如果您确实想要匹配 x 周围的零个或多个数字:使用 单词边界

\b\d*x\d*\b

将匹配 2x5x325x 甚至 x,但不会匹配 xtext 等中。

Another solution, especially if you actually do want to match zero or more digits around x: Use word boundaries.

\b\d*x\d*\b

will match 2x5, x3, 25x or even x, but it won't match the x in text etc.

总以为 2024-12-27 05:42:32

您是否尝试在季节和剧集之间匹配任意数量的 x ?如果是,请尝试

\d+x+\d+

使用 + 而不是 * 以确保季和剧集至少有一位数字,并且中间至少有一个 x

Are you trying to match any number of x in between season and episode? If yes, try

\d+x+\d+

use + instead of * to make sure there is at least one digit for season and episode and at lease one x in between.

守不住的情 2024-12-27 05:42:32

尝试使用此表达式代替“\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.

浅听莫相离 2024-12-27 05:42:32

您甚至可以使用组同时检索季节和剧集信息:

Pattern pattern = Pattern.compile(".*(\\d+)x(\\d+).*");
Matcher matcher = pattern.matcher("Series 2x08");
if (matcher.matches()) {
    int season = Integer.parseInt(matcher.group(1));
    int episode = Integer.parseInt(matcher.group(2));
    System.out.printf("Season %d, Episode %d", season, episode);
}

结果:第 2 季,第 8 集

You can even use groups to simultaneously retrieve the season and episode informations :

Pattern pattern = Pattern.compile(".*(\\d+)x(\\d+).*");
Matcher matcher = pattern.matcher("Series 2x08");
if (matcher.matches()) {
    int season = Integer.parseInt(matcher.group(1));
    int episode = Integer.parseInt(matcher.group(2));
    System.out.printf("Season %d, Episode %d", season, episode);
}

Result : Season 2, Episode 8

夜无邪 2024-12-27 05:42:32

试试这个:

\d+x\d+

+ : 1 或更多

* : 0 或更多

? : 1或根本没有

但请记住,这不是针对您的情况进行测试的最可靠的方法。如果有人输入 99x9999999,那将是第 99 季和 9999999 集。如果您想限制可能的季节和剧集数量,请检查此处。

Try this:

\d+x\d+

+ : 1 or more

* : 0 or more

? : 1 or not at all

But 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.

自找没趣 2024-12-27 05:42:32

这个怎么样?

[0-9]{1,2}x[0-9]{1,3}

假设剧集不超过 99 季,每季不超过 999 集。

How about this?

[0-9]{1,2}x[0-9]{1,3}

Assuming there are not more than 99 seasons and more than 999 episodes per season.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文