Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
471 views
in Technique[技术] by (71.8m points)

php - How to get the shortest rather than longest possible regex match with preg_match()

I have string similar to this one:

{{something1}} something2 {{something3}} something4

How can I match only "something1" using the preg_match() function?

I tried:

preg_match("/{{(.*)}}/si",$content,$matches);

but this matched too much, returning

something1}} something2 {{something3

I tried adding to the pattern, but didn't get what I want that way either.

Could you please help me with this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use non greedy modifier ? :

preg_match("/{{(.*?)}}/si",$content,$matches);
             here --^

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...