Flmhdfj
Flmhdfj

Reputation: 918

Positive lookahead doesn't stop at first occurrence

I use the regex

(?<=Charset:\s).+(?=<br\/>) 

on the following data (There is no newline char in the data, I made it look better) to capture the Charset string

<div class="box_t">Parameters</div>
<div class="box_c">Charset: i763zLFYKBqVs@nZ8PyO}N9<br/>
Input Base: 23<br/>Solution Base: 19<br/>
Timelimit: 3.1416 seconds<br/></div>

However, my match ended up being

i763zLFYKBqVs@nZ8PyO}N9<br/>
Input Base: 23<br/>
Solution Base: 19<br/>
Timelimit: 3.1416 seconds

It seems that the positive lookahead did not stop after the first occurrence. Is there a way to make it stop?

Upvotes: 25

Views: 20420

Answers (1)

sawa
sawa

Reputation: 168199

An easy way is to use the non-greedy operator: Change .+ to .+?

(?<=Charset:\s).+?(?=<br\/>)

Upvotes: 65

Related Questions