If one has the dynamic array formula FILTER():
=AVERAGE(FILTER(B2:B15,ISNUMBER(MATCH(--RIGHT(A2:A15),{2,5},0))))
![enter image description here](https://i.stack.imgur.com/YTau2.png)
If not then use this array formula:
=AVERAGE(IF(ISNUMBER(MATCH(--RIGHT(A2:A15),{2,5},0)),B2:B15))
As it is an array formula, exiting edit mode requires pressing Ctrl-Shift-Enter rather than Enter.
![enter image description here](https://i.stack.imgur.com/nVrLg.png)