Rod
Rod

Reputation: 15477

regular expression not working as i expect it too, something is wrong with Regular Expressions

(?<=atr1=\").*(?=\")

<h1 atr1="test1" atr2="test2"

I'm expecting regex to grab the value in atr1, but it is grabbing more than that. It stops at double quote after test2?

Upvotes: 0

Views: 45

Answers (3)

Rounin
Rounin

Reputation: 29511

If you want to grab

test1

from

<h1 atr1="test1" atr2="test2"

then:

atr1="([^"]+)"

will capture it.

Upvotes: 2

anubhava
anubhava

Reputation: 786146

Use negation regex and avoid lookahead:

(?<=atr1=")[^"]+

RegEx Demo

However if you're using a language like PHP, Python, etc then I suggest avoiding regex altogether and use builtin DOM parsers instead.

Upvotes: 1

Giuseppe Ricupero
Giuseppe Ricupero

Reputation: 6272

Use the lazy modifier *? to stop at the first double quote:

(?<=atr1=").*?(?=")

Online Demo

PS: i removed also the escaping for double quotes not strictly needed (unless you have to use a double quoted string)

Upvotes: 2

Related Questions