包含一个或另一个词的正则表达式 [英] Regular expression containing one word or another

查看:44
本文介绍了包含一个或另一个词的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个匹配整数后跟秒"或分钟"的表达式

I need to create an expression matching a whole number followed by either "seconds" or ""minutes"

我试过这个表达式:([0-9]+)\s+(\bseconds\b)|(\bminutes\b)

它可以在几秒钟内正常工作,但不是几分钟.

It works fine for seconds, but not minutes.

例如"5 seconds" 给出 5;seconds;而5分钟"给出;;分钟

E.g. "5 seconds" gives 5;seconds; while "5 minutes" gives ;;minutes

推荐答案

您只是错过了OR"符号的一对额外括号.以下应该可以解决问题:

You just missed an extra pair of brackets for the "OR" symbol. The following should do the trick:

([0-9]+)\s+((\bseconds\b)|(\bminutes\b))

如果没有这些,您要么匹配数字后跟秒,要么匹配单词分钟

Without those you were either matching a number followed by seconds OR just the word minutes

这篇关于包含一个或另一个词的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆