Irwell
Irwell

Reputation: 43

Regex match at least one from set of characters in specific order

I have a string which contains a sub-string of days. This sub-string contains a single character representing each applicable weekday and can contain any combination of weekdays, or the letter N where no weekday applies. Each weekday is represented by the first character, except Thursday which is an R, and weekdays must be in order.

I've tried to build a regular expression to match this substring but the regex I have written is matching blank strings.

My Regex is: ^ABC ((M?T?W?R?F?)|N) ABC$

I want this to match:

etc...

But not match:

The regex is doing this, but is also matching:

Does anyone have a quick fix?

Edit: I forgot to mention that due to limitations of the host environment I'm restricted to using the Microsoft VBScript Regular Expressions 5.5 library and it's subset.

Upvotes: 4

Views: 2704

Answers (2)

Karan Chudasama
Karan Chudasama

Reputation: 316

This can also be implemented as:

^ABC (M?T?W?R?F?|N)(?<=\S) ABC$

Upvotes: 1

anubhava
anubhava

Reputation: 785761

You can use lookahead regex:

^ABC (?=\S)(M?T?W?R?F?|N) ABC$
  • (?=\S) is to make sure next character is not space.

RegEx Demo

Upvotes: 2

Related Questions