0

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?

5
  • Could You provide expecting wrong and correct sample? Commented Jul 8, 2015 at 9:15
  • Change the (.) into a (.+) or even a (.+?) maybe. Commented Jul 8, 2015 at 9:16
  • 2
    Where do you use the expression? What language/tool? Are you sure there are slashes present in the string? Surely . is a bad idea, at least you should have tried [^"]*. Commented Jul 8, 2015 at 9:17
  • 2
    @BECRoland Don't parse XML with regex. Use an XML parser instead. Commented Jul 8, 2015 at 9:18
  • <data name=\\\"(.)\\\" xml:space=\\\"preserve\\\"> is wrong try out the answer Commented Jul 8, 2015 at 9:20

3 Answers 3

1

try this

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

no need to add \ to "

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

The OP did not specify the exact circumstances of his appliance (whether it’s a single element or an entire document). The actual issue was the missing quantifier. I made a complementary edit.
safe way always use "?" in these type of cases.
0
<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.

2 Comments

he did not state clearly the structure of his text file, it's up to him to choose the flag accordingly. so, please don't harass people who are doing their best to help with your answer, thanks
i am also trying to help. if you we have different elements it will fetch wrong nodes also. that why i informed. i will delete my prpevious comment. may be my English is not good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.