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
|
2018-06-22 09:41:56 +0800
|
C# dotnetcore
|
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;
}
2019-8-31-How-to-parse-version-range的更多相关文章
- How to parse version range
Now we are making a solution that has to get the package reference. But the version of package refer ...
- 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 ...
- agentzh 的 Nginx 教程(版本 2019.07.31)
agentzh 的 Nginx 教程(版本 2019.07.31) agentzh 的 Nginx 教程(版本 2019.07.31) https://openresty.org/download/a ...
- ARTS Challenge- Week 1 (2019.03.25~2019.03.31)
1.Algorithm - at least one leetcode problem per week(Medium+) 986. Interval List Intersections https ...
- 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
- 2020届京东秋招正式批一面记录-Java开发-2019.08.31
京东一面总结 总共时间持续时间约40分钟 1.你用过集合类里面哪些是线程安全的,哪些是线程不安全的?分别举两个例子? 线程安全:HashTable以及ConcurrentHashMap 非线程安全:A ...
- 牛客CSP-S提高组赛前集训营2 ———— 2019.10.31
比赛链接 期望得分:100+20+20 实际得分:40+20+30 awa cccc T1 :基于贪心的思路,然后开始爆搜(雾 那必然是会死的,好吧他就是死了 #include<iostrea ...
- Beta冲刺(9/7)——2019.5.31
作业描述 课程 软件工程1916|W(福州大学) 团队名称 修!咻咻! 作业要求 项目Beta冲刺(团队) 团队目标 切实可行的计算机协会维修预约平台 开发工具 Eclipse 团队信息 队员学号 队 ...
随机推荐
- CentOS7 配置163 yum源(详细步骤)
CentOS7 配置163 yum源 1)下载repo文件 wget http://mirrors.163.com/.help/CentOS7-Base-163.repo 2)备份并替换系统的r ...
- MYSQL全文索引—CONTAINS语法
我们通常在 WHERE 子句中使用 CONTAINS ,就象这样:SELECT * FROM table_name WHERE CONTAINS(fullText_column,'search con ...
- 交叉编译fw_printenv
source /opt/poky/environment... 创建交叉编译环境. 更改u-boot/tools/env/Make 添加CC 9 CC=aarch64-poky-linux-gcc - ...
- C#链式编程
一.基本链式格式 class Program { static void Main(string[] args) { Console.WriteLine("Hello World!" ...
- 不修改源代码,动态注入Java代码的方法(转)
转自:https://blog.csdn.net/hiphoon_sun/article/details/38707927 有时,我们需要在不修改源代码的前提下往一个第三方的JAVA程序里注入自己的代 ...
- MVC默认提供了一个异常过滤器 HandleErrorAttribte特性
这一篇记录MVC默认提供了一个异常过滤器 HandleErrorAttribte,下一篇介绍自定义异常过滤特性. 参考引用:https://www.cnblogs.com/TomXu/archive/ ...
- vue-router中的router-link的active-class
vue-router中的router-link的active-class 在vue-router中要使用选中样式的方法有两种: 1.直接在路由js文件中配置linkActiveClass 2.在r ...
- reduce 好东西
reduce()方法可以搞定的东西,for循环,或者forEach方法有时候也可以搞定,那为啥要用reduce()?这个问题,之前我也想过,要说原因还真找不到,唯一能找到的是:通往成功的道路有很多,但 ...
- linux(一)vi和vim
vi 多模式文本编辑器 多模式产生的原因 四种模式 正常模式 插入模式 命令模式 可视模式 vi man vi vim vim正常模式 直接vim回车,或vim空格文件名回车 i进入插入模式 I(sh ...
- C# ArrayList、HashSet、HashTable、List、Dictionary的区别
在C#中,数组由于是固定长度的,所以常常不能满足我们开发的需求. 由于这种限制不方便,所以出现了ArrayList. ArrayList.List<T> ArrayList是可变长数组,你 ...