Regular Expressions 101

Community Patterns

too greedy +

0

Regular Expression
PCRE (PHP <7.3)

/
(\w\+)?(\w+)(\w\+)?
/
g

Description

I would like to make that pattern match word char followed by plus folowed by few word chars and followed by another word char and plus. Both word chars followed by plus have to be optional. The problem is that + in middle part takes as much chars as it can and last part never matches (for example s+aaas+ doesn't match). There HAVE to be these three groups so (\w+)?(\w+)(+)? is bad for me. Any idea?

Submitted by Nel - 10 years ago