omega
omega

Reputation: 43863

How to force parts of regex to match?

I have this regex

<SharePointWebControls:(\w+).*?fieldname="(.*?)".*?\s*<\/SharePointWebControls:(?:\w+).*?>

Notice the two parts of it (\w+) and (?:\w+). This regex will match somethign even when these two are different. How can I force it so the matched area has to be the same for these two groups?

Thanks

Upvotes: 2

Views: 26

Answers (1)

anubhava
anubhava

Reputation: 785256

You can use back-reference to matched groups using \1:

<SharePointWebControls:(\w+).*?fieldname="(.*?)".*?\s*<\/SharePointWebControls:\1>

Here \1 in later part is actually back-reference to first capturing group which is being captured by (\w+).

RegEx Demo

Upvotes: 3

Related Questions