正则表达式逗号或逗号空间或空格 [英] Regex Comma or Comma Space or Space

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

问题描述

我的问题是[,\ s |,| \ s]会匹配," as,",并留出多余的空间

My problem is the [,\s|,|\s] will match ", " as "," and leave a extra space

所以我没有匹配"Sat,Mon":

So I do not get a match "Sat, Mon" with:

(Thu|Fri|Sat)[,\s|,|\s](Mon|Tue)

通过匹配(Thu | Fri | Sat)[,\ s |,| \ s]我在星期六"上找到了匹配项,但match.Value在星期六"上(没有空格)

By matching on (Thu|Fri|Sat)[,\s|,|\s] I get a match on "Sat, " but the match.Value is on "Sat," (no space)

基本上我也想在"Sat,Mon","Sat,Mon","Sat Mon"而不是"SatMon"上进行比赛

Basically I want to also get a match on "Sat,Mon" "Sat, Mon" "Sat Mon" but not "SatMon"

谢谢

推荐答案

(Thu|Fri|Sat)[,\s]\s*(Mon|Tue)

这将允许逗号或空格以及 Mon Tue

This will allow comma or space and any additional space before Mon or Tue

您的版本将字符类和替换的概念混为一谈.用 | 分隔选项的替代必须在括号内.我们可以使用(?:)语法使这些括号不被捕获.

Your version was conflating the notions of character classes and alternation. Alternation, where you separate options with | must be inside of parentheses. We can make these parentheses non-capturing using the (?: ) syntax.

以上,我使用了字符类.要使用交替:

Above, I used a character class. To use alternation:

(Thu|Fri|Sat)(?:,|\s)\s*(Mon|Tue)

我已经使用 \ s 表示空格,但是出于您的目的,您可以将它们替换为文字空间.

I have used \s to denote whitespace, but for your purposes you could replace them with a literal space.

这篇关于正则表达式逗号或逗号空间或空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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