BEC Roland
BEC Roland

Reputation: 25

Regular expression for XML element with arbitrary attribute value

I'm not very confortable with RegEx.

I have a text file with a lot of data and different formats. I want to keep this kind of string.

<data name=\"myProptertyValue\" xml:space=\"preserve\">

Only the value of the name property can change.

So I imagined a regex like this <data name=\\\"(.)\\\" xml:space=\\\"preserve\\\"> but it's not working.

Any tips?

Upvotes: 0

Views: 505

Answers (3)

dakab
dakab

Reputation: 5875

Your (.) will capture only a single character; add a quantifier like + (“one or more”):

/<data name=\\"(.+)\\" xml:space=\\"preserve\\">/

Depending on what exactly your input is (element by element or entire document) and on what you want to achieve (removing/replacing/testing/capturing), you should make the regex global (by adding the g flag), so it is applied not only once. Also, you should make the + quantifier lazy by adding a ? to it. That will make it non-greedy, because you want capturing to stop at the ending quote of the attribute (like all but quotation mark: [^"]). Then, it will look like this:

/<data name=\\"(.+?)\\" xml:space=\\"preserve\\">/g

Upvotes: 0

Raghavendra
Raghavendra

Reputation: 3580

try this

<data name=\\".*?\\" xml:space=\\"preserve\\">

no need to add \ to "

Upvotes: 1

lu.d
lu.d

Reputation: 1

<data name=\\"(.+)\\" xml:space=\\"preserve\\">

It will catch what's inside "data name".

If you're having trouble with regex, using this kind of sites to construct your regex can help you : https://regex101.com/ , http://regexr.com/ etc.

Upvotes: 0

Related Questions