-2

I am trying to learn Regex construction, and i am stuck in one problem.

Problem Statement: A regex that should match following phrases:

rap them
tapeth
apth
wrap/try
sap tray
87ap9th
apothecary

but should not match

aleht
happy them
tarpth
Apt
peth
tarreth
ddapdg
apples
shape the

My attempt:

^[a-z0-9]+p.?t

My regex is matching tarpth as well. How can i fix it?

Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72

3 Answers3

2

This meets this requirement:

^([a-z0-9]+p.{1}|[a-su-z0-9]+p)t

Demo:

https://regex101.com/r/yY3lV6/1

n-dru
  • 9,285
  • 2
  • 29
  • 42
2

This should do it - you failed to require a before the p:

.*ap.?t.*
Armand
  • 23,463
  • 20
  • 90
  • 119
1

Looks like this regex does the trick, it's a very specific question thought:

(ap)(.?)t
Carlo Moretti
  • 2,213
  • 2
  • 27
  • 41