开发者

Replace with use of regular expressions in Visual Studio: unexpected results

开发者 https://www.devze.com 2023-03-13 02:40 出处:网络
I have to move some hard-coded strings to external resources files, and I\'m trying to automate the proccess a little bit using regular expressions.

I have to move some hard-coded strings to external resources files, and I'm trying to automate the proccess a little bit using regular expressions.

I have some difficulties getting this to work, though.

Sample code excerpt:

    public List<string> GetAllProductIDs()
    {
        var doc = XElement.Load(Path + @"\light.xml");
        var elements = doc.Element("products").Elements("product");
        // (...)

Regular expressions that I use:

  • find what = "{([^"\r]*)}"

(find a string that starts and ends with double apostrophes, and does not contain another double apostrophe, nor a line break, in the middle; and capture it without the first and the last double apostrophe)

  • replace with = Resources.\1

My intention being that

  • Element("products")

would become

  • Element(Resources.products)

What happens is

EXPECTED RESULT: the regex does match var doc = XElement.Load(Path + @"\light.xml");

UNEXPECTED RESULT: the regex does match *var elements = doc.Element("products ").Elements(" product"); (which is something I can cope with, I'd just skip it manually)

But it does NOT match "products" nor "p开发者_JAVA百科roduct"

Why?


The built in Visual Studio regex differs than the more common regex flavors.

The problem is \r is being interpreted literally rather than as the return control character. There are only a handful of control characters that appear to be supported by the Visual Studio regex flavor and \r isn't one of them. Therefore, the engine is using it literally.

To verify this, change \r to \a and you'll see that it matches "products" and "product" just fine. The reason is that the literal character "a" doesn't exist in those words, so the match succeeds, whereas the \r made the match fail since the letter "r" does exist in those words.

To get around this issue you can refer to the return control character by its Unicode hexadecimal value, which is 000D. Change \r to \u000D or to \x0D:

"{([^"\u000D]*)}"

or

"{([^"\x0D]*)}"

You might be interested in checking out the Visual Studio Productivity Power Tools, which supports the .NET Regex flavor that most people are used to. Using that tool you would use:

  • Pattern: "([^"\r]*)" note that \r works as expected in this case.
  • Replacement: Resources.$1


I'm trying to replace xml:

Replace:

<metaKeyword2>Avaeon.Topoix.Gravity.Add</metaKeyword2>

with this

<metaKeywords>
      <metaKeyword>
        <keyword>Avaeon.Topoix.Gravity.Add</keyword>
      </metaKeyword>
    </metaKeywords>

The value within the attribute is variable, I cannot seem to get it, I suspect the XML's < > are breaking it, here's what I've tried

<metaKeyword2>:w</metaKeyword2>

with

<metaKeywords>
      <metaKeyword>
        <keyword>\1</keyword>
      </metaKeyword>
    </metaKeywords>

but it finds nothing.

0

精彩评论

暂无评论...
验证码 换一张
取 消