Now we are making a solution that has to get the package reference. But the version of package reference is a range and the default version parser need input a version but not a version range.

This post will tell you how to parse the version range string to reference version.




The format for reference version is like this

[2.1.0.293,3.0)
[1.1.0.34,2.0)
(1.1.0.34,2.0]
2.1

For parse the reference version string, we should make some property.


public class ReferenceVersion
{
public ReferenceVersion(Version version)
{
Version = version;
MinVersion = version;
MaxVersion = version;
IsIncludeMaxVersion = true;
IsIncludeMinVersion = true;
} public ReferenceVersion(Version minVersion, Version maxVersion, bool isIncludeMinVersion,
bool isIncludeMaxVersion)
{
Version = null;
MinVersion = minVersion;
MaxVersion = maxVersion;
IsIncludeMinVersion = isIncludeMinVersion;
IsIncludeMaxVersion = isIncludeMaxVersion;
} public Version Version { get; } public Version MinVersion { get; } public Version MaxVersion { get; } public bool IsIncludeMinVersion { get; } public bool IsIncludeMaxVersion { get; }
}

I will use regex to get the string and parse the string to version.

      public static ReferenceVersion Parser(string str)
{
if (_regex == null)
{
_regex = new Regex(@"([(|\[])([\d|.]*),([\d|.]*)([)|\]])", RegexOptions.Compiled);
} var res = _regex.Match(str); if (res.Success)
{
var isIncludeMinVersion = res.Groups[1].Value;
var minVersion = res.Groups[2].Value;
var maxVersion = res.Groups[3].Value;
var isIncludeMaxVersion = res.Groups[4].Value; return new ReferenceVersion
(
string.IsNullOrEmpty(minVersion) ? null : Version.Parse(minVersion),
string.IsNullOrEmpty(maxVersion) ? null : Version.Parse(maxVersion),
isIncludeMinVersion.Equals("["),
isIncludeMaxVersion.Equals("]")
);
} return new ReferenceVersion(Version.Parse(str));
} private static Regex _regex;

We can get the reference version in the solution file and know the solution reference package.

Full code:

    /// <summary>
/// 引用的版本
/// 用来转换 [2.1.0.293,3.0)、 (1.1.0.3,2.0]、 5.2 的版本
/// </summary>
public class ReferenceVersion
{
/// <summary>
/// 创建引用版本
/// </summary>
/// <param name="version">版本</param>
public ReferenceVersion(Version version)
{
Version = version;
MinVersion = version;
MaxVersion = version;
IsIncludeMaxVersion = true;
IsIncludeMinVersion = true;
} /// <summary>
/// 创建引用版本
/// </summary>
/// <param name="minVersion">最低版本</param>
/// <param name="maxVersion">最高版本</param>
/// <param name="isIncludeMinVersion">是否包括最低版本</param>
/// <param name="isIncludeMaxVersion">是否包括最高版本</param>
public ReferenceVersion(Version minVersion, Version maxVersion, bool isIncludeMinVersion,
bool isIncludeMaxVersion)
{
Version = null;
MinVersion = minVersion;
MaxVersion = maxVersion;
IsIncludeMinVersion = isIncludeMinVersion;
IsIncludeMaxVersion = isIncludeMaxVersion;
} /// <summary>
/// 版本
/// </summary>
public Version Version { get; } /// <summary>
/// 最低版本
/// </summary>
public Version MinVersion { get; } /// <summary>
/// 最高版本
/// </summary>
public Version MaxVersion { get; } /// <summary>
/// 是否包括最低版本
/// </summary>
public bool IsIncludeMinVersion { get; } /// <summary>
/// 是否包括最高版本
/// </summary>
public bool IsIncludeMaxVersion { get; } /// <summary>
/// 转换版本
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static ReferenceVersion Parser(string str)
{
if (_regex == null)
{
_regex = new Regex(@"([(|\[])([\d|.]*),([\d|.]*)([)|\]])", RegexOptions.Compiled);
} var res = _regex.Match(str); if (res.Success)
{
var isIncludeMinVersion = res.Groups[1].Value;
var minVersion = res.Groups[2].Value;
var maxVersion = res.Groups[3].Value;
var isIncludeMaxVersion = res.Groups[4].Value; return new ReferenceVersion
(
string.IsNullOrEmpty(minVersion) ? null : Version.Parse(minVersion),
string.IsNullOrEmpty(maxVersion) ? null : Version.Parse(maxVersion),
isIncludeMinVersion.Equals("["),
isIncludeMaxVersion.Equals("]")
);
} return new ReferenceVersion(Version.Parse(str));
} private static Regex _regex;
}

我搭建了自己的博客 https://blog.lindexi.com/ 欢迎大家访问,里面有很多新的博客。只有在我看到博客写成熟之后才会放在csdn或博客园,但是一旦发布了就不再更新

如果在博客看到有任何不懂的,欢迎交流,我搭建了 dotnet 职业技术学院 欢迎大家加入


本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系

How to parse version range的更多相关文章

  1. t default] Failed to discover available identity versions when contacting http://ahswj-cloud-controller:35357. Attempting to parse version from URL.: ConnectFailure

    2018-09-13 21:39:20.778 80758 WARNING keystoneauth.identity.generic.base [req-ea24b7ad-5aee-44b2-b68 ...

  2. Unable to create Debug Bridge:Unable to start adb server:error:cannot parse version

    打开Android Studio时报如下错误提示: Unable to create Debug Bridge:Unable to start adb server:error:cannot pars ...

  3. error: checker javascript/jshint: can’t parse version string (abnormal termination?)”

    vim 安装插件(k-vim方法 )好后 编辑js文件提示错误 可能是nodejs环境没搭建好 或者版本有误 用nvm安装node 后 需要 source ~/.bashrc 或者重新开一个终端 再运 ...

  4. androidstudio提示adb错误:cannot parse version string:kg01的解决方法

    打开adb.exe的文件目录,同时按下shift和鼠标右键,打开cmd后运行一下这个命令adb kill-server

  5. 2019-8-31-How-to-parse-version-range

    title author date CreateTime categories How to parse version range lindexi 2019-08-31 16:55:58 +0800 ...

  6. 【转】 svn 错误 以及 中文翻译

    直接Ctrl+F 搜索你要找的错 # # Simplified Chinese translation for subversion package # This file is distribute ...

  7. npm-package.json

    Specifics of npm's package.json handling DESCRIPTION§ This document is all you need to know about wh ...

  8. RFC-RTSP

    Network Working Group H. Schulzrinne Request for Comments: 2326 Columbia U. Category: Standards Trac ...

  9. RTSP Spectification

    Refer: https://www.ietf.org/rfc/rfc2326.txt Network Working Group H. SchulzrinneRequest for Comments ...

随机推荐

  1. java读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  2. 【AtCoder Regular Contest 092】C.2D Plane 2N Points【匈牙利算法】

    C.2D Plane 2N Points 题意:给定N个红点二维坐标N个蓝点二维坐标,如果红点横纵坐标都比蓝点小,那么它们能够构成一组.问最多能构成多少组. 题解:把满足要求的红蓝点连线,然后就是匈牙 ...

  3. jenkins使用教程!

    http://jenkins-ci.org/ 首先去官方下载war包,直接安装jenkins的方式比较麻烦. 下载tomcat,jdk和ant cd /optwget http://mirrors.h ...

  4. UVA_488:Triangle Wave

    PS:The input begins with a single positive integer on a line by itself indicating the number of the ...

  5. Gym - 101480D_Digit Division

    题意:输入n,m,将n分段,每一段都可以被m整除,有多少种方法. 题解:找到n最多可以分成多少段,1段1中分法,2段2中分法,3段4种分法--计算可知若有x段则2^x-1种分法. 注意:如果n无法被m ...

  6. Python基础:18类和实例之二

    1:绑定和非绑定 当存在一个实例时,方法才被认为是绑定到那个实例了.没有实例时方法就是未绑定的.在很多情况下,调用的都是一个绑定的方法. 调用非绑定方法并不经常用到,其中一个主要的场景是:派生一个子类 ...

  7. ORACLE学习笔记-ORACLE(基本命令)

    --查看VGA信息: show sga; select * from v$sgastat;--可以通过以下几个动态性能视图查看信息: V$sysstat                系统统计信息 V ...

  8. MapReduce数据流-输出

  9. BigData NoSQL —— ApsaraDB HBase数据存储与分析平台概览

    一.引言 时间到了2019年,数据库也发展到了一个新的拐点,有三个明显的趋势: 越来越多的数据库会做云原生(CloudNative),会不断利用新的硬件及云本身的优势打造CloudNative数据库, ...

  10. @bzoj - 3749@ [POI2015] Łasuchy

    目录 @description@ @solution@ @version - 1@ @version - 2@ @accepted code@ @version - 1@ @version - 2@ ...