Crab Bucket
Crab Bucket

Reputation: 6277

Regex matching terminating quote only if quote at the beginning

I want to match the following element with regex

target="#MOBILE"

and all valid variants.

I've written the regex

target[\s\S]*#MOBILE[^>^\s]*

which matches the following

target="#MOBILE"
target = "#MOBILE"
target=#MOBILE
target="#MOBILE" (followed directly by >)

but it doesn't match

target=" #MOBILE " 

properly (note the extra space). It only matches

target=" #MOBILE 

missing out the final quote

What I need is the terminating expression [^>^\s]* to match a quote only if it matches a quote at the beginning. It also needs to work with single quotes. The terminating expression also needs to end with a whitespace or > char as it does currently.

I'm sure there is a way to do this - but I'm not sure how. It's probably standard stuff - I just don't know it

Incidently I'm not sure that [^>^\s]* is the best way to terminate if the regex hits a space or > char but it's the only way that I can get it to work.

Upvotes: 4

Views: 416

Answers (3)

Zsolt Botykai
Zsolt Botykai

Reputation: 51663

Try the following if you need to check that your quotes are in pairs:

target\s*=\s*(['"])(?=\1)\s*#MOBILE\s*(?<=\1)\1

But it really depends if your regex engine supports positive look-(ahead|behind) syntax. And if it supports back-referencing.

Upvotes: 1

Kobi
Kobi

Reputation: 138127

You can use a backreference, similar to jensgram's suggestion:

target\s*=\s*(?:(")\s*)?#Mobile\s*\1

(?:(")\s*)? - Optional non-capturing group that contains a quote (which is captured), and additional optional spaces. If it matched, \1 will contain a quote.

Working example: http://regexr.com?2vkkq

A better alternative for .Net (mainly because you want single quotes, and \1 behaves differently for uncaptured groups):

target\s*=\s*(["']?)\s*?#Mobile\s*\1

Working example: Regex Storm

Upvotes: 2

ngsiolei
ngsiolei

Reputation: 614

Without quotes target\s*=\s*#MOBILE

With double quotes target\s*=\s*"\s*#MOBILE\s*"

With single quotes target\s*=\s*'\s*#MOBILE\s*'

All together

(target\s*=\s*#MOBILE)|(target\s*=\s*"\s*#MOBILE\s*")|(target\s*=\s*'\s*#MOBILE\s*')

Or someone can make it neater.

Upvotes: 0

Related Questions