Score:0

Need a regular expression to match 3-member simple series

br flag

I need a regex that would match the following example strings:

 Example 1: "red, blue, and hot"
 Example 2: "red, blue, or hot"

Three individual words, the first two followed by commas, and the conjunction 'and' (case 1) alternatively 'or' (case 2).

Case 1: "<word01>, <word02>, and <word03>"
Case 2: "<word01>, <word02>, or <word03>"

I have the following regex (\w+,(\s|\n)+){2}(and|or) that successfully matches the strings however it will also match strings where my example expressions are contained within the string. I only want a successful match if the string contains my example expression and nothing else.

In other words green, red, blue, and hot should not match as it contains an additional word and comma before the match.

Romeo Ninov avatar
in flag
You can try `(\w+,(\s|\n)+){2,10}(and|or)` to match between 3 and 11 times :)
Tomáš Bažant avatar
br flag
Bu that's not what i want :-) i dont want to match list with 3 and more members, but exactly 3 in total, such as "Tomas, Paul, and Eve". Longer lists should be skipped.
Tomáš Bažant avatar
br flag
In fact, i dont want any ", " before the regexp, otherwise anything can be there. But mere `[^,]\s` does not work somehow...
Score:0
aq flag

REGEX Variations

I gave you a few variations depending how you want to deal with punctuation.

RegEx ➡ (^\w+\,\s+\w+\,\s+(and|or)\s+\w+\Z)                               
Match Test String
red, blue, and hot
red, blue, and hot.
green, red, blue, and hot
red, blue, and hot, but not green

 

RegEx ➡ (^\w+\,\s+\w+\,\s+(and|or)\s+\w+\.\Z)                         
Match Test String
red, blue, and hot
red, blue, and hot.
green, red, blue, and hot
red, blue, and hot, but not green

 

RegEx ➡ (^\w+\,\s+\w+\,\s+(and|or)\s+\w+\.?\Z)                         
Match Test String
red, blue, and hot
red, blue, and hot.
green, red, blue, and hot
red, blue, and hot, but not green

 

REGEX Used

Regex Description                                                                          
    ^ start of the string (position)
    \Z end of the string (position)
    + previous token > 0 times
    ? previous token < 2 times
    \w any single word character
    \, comma character
    \s whitespace character
    | separates alternatives (or)
    \. period character
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.