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 团队信息 队员学号 队 ...
随机推荐
- sass的语法及其用法
1.sass语法 1.1 css的编译模式 css --- 普通 sass / scss --- 高效 // ********* less --- 高效 1.2 sass介绍 来源: ruby语言 基 ...
- NHibernet Unable to locate persister for the entity
第一 xml文件必须为 *.hbm.xml 第二 设置xml文件为嵌入的资源,用鼠标点击右键 然后生成操作里 选择嵌入的资源即可解决. https://www.cnblogs.com/lyj/
- idea spring boot搭建笔记
如何建立spring boot项目 首先建一个empty Probject,Modules导入新创建的Project new modules选择Spring lnitializr ->next( ...
- Es学习第二课, ES安装和客户端使用
Elasticsearch安装依赖于jdk,所以大家先保证自己电脑安装好Java环境(JDK7或更高版本),并配置好环境变量:这块的配置我就不细讲了,不管是Linux还是Windows,这块的安装和配 ...
- 2PC和3PC
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11484077.html 2PC 是计算机网络尤其是在数据库领域内,为了使基于分布式系统架构下的所有节点 ...
- Integer之常用方法
public static int parseInt(String s, int radix) throws NumberFormatException {} // radix进制 s 换成 10进制 ...
- sql 基础语法 alter用法和视图,透视
--查询没有被删除的学生 alter table StuInfo --修改列属性 alter column isdelete bit null alter table StuInfo --删除列 dr ...
- kubernetes使用kubeadm升级集群
升级前准本 官网: https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm-upgrade/查看可升级的组件 [root@h ...
- MySQL-存储过程动态执行sql
存储过程动态执行 sql --存储过程名和参数,参数中in表示传入参数,out标示传出参数,inout表示传入传出参数 create procedure p_procedurecode(in sumd ...
- Dubbo入门到精通学习笔记(二十):MyCat在MySQL主从复制的基础上实现读写分离、MyCat 集群部署(HAProxy + MyCat)、MyCat 高可用负载均衡集群Keepalived
文章目录 MyCat在MySQL主从复制的基础上实现读写分离 一.环境 二.依赖课程 三.MyCat 介绍 ( MyCat 官网:http://mycat.org.cn/ ) 四.MyCat 的安装 ...