简体   繁体   English

提取两个双引号之间的所有数据

[英]Extract all data in between two double quotes

I'm trying to use a powershell regex to pull version data from the AssemblyInfo.cs file.我正在尝试使用 powershell 正则表达式从 AssemblyInfo.cs 文件中提取版本数据。 The regex below is my best attempt, however it only pulls the string [assembly: AssemblyVersion(" . I've put this regex into a couple web regex testers and it LOOKS like it's doing what I want, however this is my first crack at using a powershell regex so I could be looking at it wrong.下面的正则表达式是我最好的尝试,但它只提取字符串[assembly: AssemblyVersion(" 。我已经将这个正则表达式放入几个网络正则表达式测试器中,看起来它正在做我想做的事,但这是我的第一次破解使用 powershell regex,所以我可能看错了。

$s = '[assembly: AssemblyVersion("1.0.0.0")]'
$prog = [regex]::match($s, '([^"]+)"').Groups[1].Value

You also need to include the starting double quotes otherwise it would start capturing from the start until the first " is reached.您还需要包含起始双引号,否则它会从头开始捕获,直到到达第一个"为止。

$prog = [regex]::match($s, '"([^"]+)"').Groups[1].Value
                            ^

Try this regex "([^"]+)"试试这个正则表达式"([^"]+)"

Regex101 Demo Regex101 演示

Regular expressions can get hard to read, so best practice is to make them as simple as they can be while still solving all possible cases you might see.正则表达式可能难以阅读,因此最佳做法是使它们尽可能简单,同时仍能解决您可能看到的所有可能情况。 You are trying to retrieve the only numerical sequence in the entire string, so we should look for that and bypass using groups.您正在尝试检索整个字符串中唯一的数字序列,因此我们应该寻找它并绕过使用组。

$s = '[assembly: AssemblyVersion("1.0.0.0")]'
$prog = [regex]::match($s, '[\d\.]+').Value

$prog $prog

1.0.0.0

For the generic solution of data between double quotes, the other answers are great.对于双引号之间数据的通用解决方案,其他答案都很棒。 If I were parsing AssemblyInfo.cs for the version string however, I would be more explicit.但是,如果我为版本字符串解析 AssemblyInfo.cs,我会更明确。

$versionString = [regex]::match($s, 'AssemblyVersion.*([0-9].[0-9].[0-9].[0-9])').Groups[1].Value
$version = [version]$versionString

$versionString $versionString

1.0.0.0

$version $版本

Major  Minor  Build  Revision
-----  -----  -----  --------
1      0      0      0    

Update/Edit:更新/编辑:

Related to parsing the version (again, if this is not a generic question about parsing text between double quotes) is that I would not actually have a version in the format of Mmbr in my file because I have always found that Major.minor are enough, and by using a format like 1.2.* gives you some extra information without any effort.与解析版本相关(同样,如果这不是关于在双引号之间解析文本的通用问题)是我的文件中实际上不会有Mmbr格式的版本,因为我一直发现 Major.minor 就足够了, 并且通过使用像1.2.*这样的格式,可以毫不费力地为您提供一些额外的信息。

See Compile date and time and Can I generate the compile date in my C# code to determine the expiry for a demo version?请参阅编译日期和时间以及是否可以在我的 C# 代码中生成编译日期以确定演示版本的到期日? . .

When using a * for the third and fourth part of the assembly version, then these two parts are set automatically at compile time to the following values:当对汇编版本的第三和第四部分使用 * 时,这两个部分会在编译时自动设置为以下值:

third part is the number of days since 2000-01-01第三部分是自 2000-01-01 以来的天数

fourth part is the number of seconds since midnight divided by two (although some MSDN pages say it is a random number)第四部分是从午夜开始的秒数除以 2(虽然一些 MSDN 页面说它是一个随机数)

Something to think about I guess in the larger picture of versions, requiring 1.2.*, allowing 1.2, or 1.2.3, or only accepting 1.2.3.4, etc.我想在更大的版本图中需要考虑一些事情,需要 1.2.*,允许 1.2 或 1.2.3,或者只接受 1.2.3.4 等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM