How to parse version range
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的更多相关文章
- 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 ...
- 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 ...
- error: checker javascript/jshint: can’t parse version string (abnormal termination?)”
vim 安装插件(k-vim方法 )好后 编辑js文件提示错误 可能是nodejs环境没搭建好 或者版本有误 用nvm安装node 后 需要 source ~/.bashrc 或者重新开一个终端 再运 ...
- androidstudio提示adb错误:cannot parse version string:kg01的解决方法
打开adb.exe的文件目录,同时按下shift和鼠标右键,打开cmd后运行一下这个命令adb kill-server
- 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 ...
- 【转】 svn 错误 以及 中文翻译
直接Ctrl+F 搜索你要找的错 # # Simplified Chinese translation for subversion package # This file is distribute ...
- npm-package.json
Specifics of npm's package.json handling DESCRIPTION§ This document is all you need to know about wh ...
- RFC-RTSP
Network Working Group H. Schulzrinne Request for Comments: 2326 Columbia U. Category: Standards Trac ...
- RTSP Spectification
Refer: https://www.ietf.org/rfc/rfc2326.txt Network Working Group H. SchulzrinneRequest for Comments ...
随机推荐
- mysql 使用concat模糊查询
如果这三个字段中有值为NULL,则返回的也是NULL,那么这一条记录可能就会被错过,使用IFNULL进行判断 SELECT * FROM `magazine` WHERE CONCAT(IFNULL( ...
- iOS 后台定位
http://www.cocoachina.com/ios/20150724/12735.html 前言 之前的文章说过 我现在做的是LBS定位的社交APP 其中主要的一个功能就是能够实时定位社交圈中 ...
- redis :read error on connection
最近做了一个多人竞拍的小功能 因为以前没做过 所以踩了很多坑用的是 mysql + php + redis 实现的竞拍功能 这里先说一下踩得第二个坑redis 的原因 真是欲哭无泪 解决完一个 ...
- JavaScript for循环 while循环
循环可以将代码块执行指定的次数. JavaScript 循环 如果您希望一遍又一遍地运行相同的代码,并且每次的值都不同,那么使用循环是很方便的. 我们可以这样输出数组的值: 一般写法: documen ...
- 【Leetcode链表】两两交换链表中的节点(24)
题目 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表.你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2->3->4, 你应该返回 2- ...
- 从零学React Native之07View
View 组件是React Native最基本的组件.绝大部分其他React Native 组件. View组件的颜色和边框 backgroundColor 键用来指定颜色. RN 0.19版本开始, ...
- Android横竖屏切换和灭屏亮屏时Activity的生命周期探究(1)
研究这个问题的初衷在于项目中碰到了一个问题:横屏的时候灭屏再亮屏,亮屏的时候用户能够清晰的看到先启动竖屏(过程1)再切换到横屏的过程,因为灭屏的时候onSaveInstanceState()保存的时横 ...
- HashMap的运用 计算3个班级的总成绩和平均成绩
TestStudent.java package com.sxt.home; import java.util.ArrayList; import java.util.HashMap; import ...
- 补充:css制作三角
梯形图案看下面这段样式: .test{width:10px; height:10px; border:10px solid; border-color:#ff3300 #0000ff #339966 ...
- Android Http实现文件的上传和下载
最近做一个项目,其中涉及到文件的上传和下载功能,大家都知道,这个功能实现其实已经烂大街了,遂.直接从网上荡了一堆代码用,结果,发现网上的代码真是良莠不齐,不是写的不全面,就是有问题,于是自己重新整理了 ...