Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Comma or Comma Space or Space

Tags:

.net

regex

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

like image 778
paparazzo Avatar asked Sep 14 '25 04:09

paparazzo


1 Answers

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

like image 75
Jay Avatar answered Sep 15 '25 19:09

Jay