Regex Comma or Comma Space or Space

牧云@^-^@ 提交于 2021-01-27 11:55:11

问题


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

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

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

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

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

Thanks


回答1:


(Thu|Fri|Sat)[,\s]\s*(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)

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




回答2:


Try the following \s?[, ]\s?




回答3:


To build on @Jay answer. I would simply use a greedy match.

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


来源:https://stackoverflow.com/questions/10341689/regex-comma-or-comma-space-or-space

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!