tqwer
tqwer

Reputation: 93

PCRE regexp contains '.' matche all string instead of a part

here is my regexp:

/bar.*\d\sfoo/

input string:

bar something 1 foo bar something 2 foo bar

it's matches:

bar something 1 foo bar something 2 foo

instead of:

bar something 1 foo
bar something 2 foo

Is there way to solve this ?
Note: something is any characters, except new line.

Upvotes: 1

Views: 240

Answers (2)

aioobe
aioobe

Reputation: 420951

You should make the * reluctant, or lazy, by adding a ? after it:

/bar.*?\d\sfoo/

Upvotes: 3

DhruvPathak
DhruvPathak

Reputation: 43235

You should use "lazy" operator "?" to match minimum of the pattern,else it matches till maximum match. Here is corrected version :

/bar.*?\d\sfoo/

See here : http://regexr.com?309sa

Upvotes: 0

Related Questions