Lucky500
Lucky500

Reputation: 507

Regex to match a letter, but not if that letter starts the word

I've been trying to figure this one out at regex101, but no luck yet. I want to match the second 's' in system for instance, but not if the s is at the start of the word. so I want to match the "s" in mos, or answer, but I dont want to match the s in space.

This is what I have tried so far:

s*(?<!\W)

with couple variations, but no luck yet.

Upvotes: 0

Views: 29

Answers (1)

cs95
cs95

Reputation: 402483

A negative lookbehind is the way to go. I think you need this:

(?<!\b)s

Details

  • (?<!...) - negative lookbehind
  • \b - word boundary
  • s - the letter s to be matched (will not be matched if there is a word boundary before)

Regex101 demo.

Upvotes: 2

Related Questions