开发者

Replace a substring using RegEx

Here is a line in my xyz.csproj file:

<Reference Include="SomeDLLNameHere, Version=10.2.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />

All I need to do is replace the 'Version=10.2.6.0' to 'Version=11.0.0.0' .

The program I need to do this in is VSBuild which uses VBScript so I believe.

The problem is that I can't hardcode the 'old' version numbe开发者_开发技巧r. I therefore need to replace the following :

<Reference Include="SomeDLLNameHere, Version=10.2.6.0,

I therefor need a regex that will match the above bearing in mind that that in the example quoted, the 10.2.6.0 could be anything.

I believe that a regex that would select the text including and between

'<Reference Include="SomeDLLNameHere' and '>' is what I need.

There are other references to similar requests but none seem top work for me.

I would normally use C# to do this sort of thing and VBScript/Regex is something I avoid like the plague.


For most regex flavors, you would use this:

<Reference Include="SomeDLLNameHere.*?/>

For visual studio, I am not sure if the *? would work... Try this:

\<Reference Include="SomeDLLNameHere[^/]*\/\>


This regex pattern should work

"(<Reference[^>]+Version=)([^,]+),"

Applied with VBScript

  str1 = "<Reference Include=""SomeDLLNameHere, Version=10.2.6.0,"

  ' Create regular expression.
  Set regEx = New RegExp
  regEx.Pattern = "(<Reference[^>]+Version=)([^,]+),"

  ' Make replacement.
  ReplaceText = regEx.Replace(str1, "$111.0.0.0,")
  WScript.echo ReplaceText

Gives the correct result

<Reference Include="SomeDLLNameHere, Version=11.0.0.0,

UPDATE

if you need something that matches between Version= and the end of the tag use > instead of ,

"(<Reference[^>]+Version=)([^>]+)>"


Using Regex with C# or VBScript is pretty much the same because it all comes to developing the regular expression. Something like this could help: <Reference\s+Include\s*=\s*\".+\",\s*Version\s*=\s*.+,

Not sure what are the rules about case sensitivity and white spaces in csproj files, but this covers the form you described previously. Note that the "+" operator means one or kleen.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜