我有以下网址 www.localproject.com:843/user/validate/eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9eyJ0ZW1wVXJsIjoie1wiQ3VzdG9tZXJJZFwiOjEsXCJDb3Vyc2VJZFwiOjEsXCJUb2tlblwiOm51bGwsXCJFeHBpcnlcIjpcIjIwMTgtMDQtMThUMTc6MzU6MTMuOTQ2MjM2NCswNTowMFwifSJ9uvm7jZ3us5UFa1hqh4bod2cSamcxF2rRUbfxs7DHQs
在验证(包括数字等)后,我需要从中提取10到30个字符。例如,我只需要这个
eyJhbGciOiJodHRwOi8vd3d
什么应该是正则表达式?我试过跟随,但它不起作用
^api/user/validate/(^([a-zA-Z\d]){50})
答案 0 :(得分:0)
答案 1 :(得分:0)
您可以使用正则表达式或Substring
执行此操作。如果字符串存储在web.config中,您仍然可以使用ConfigurationManager访问它。我建议从Uri
中取出不需要的部分。这样一旦你部署到prod,就会有更少的机会崩溃。
var input = new Uri("www.localproject.com:843/user/validate/eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9eyJ0ZW1wVXJsIjoie1wiQ3VzdG9tZXJJZFwiOjEsXCJDb3Vyc2VJZFwiOjEsXCJUb2tlblwiOm51bGwsXCJFeHBpcnlcIjpcIjIwMTgtMDQtMThUMTc6MzU6MTMuOTQ2MjM2NCswNTowMFwifSJ9uvm7jZ3us5UFa1hqh4bod2cSamcxF2rRUbfxs7DHQs");
var environmentAgnosticPath = input.Segments[input.Segments.Length - 1];//take just the part after /validate
//example output: eyJhbGciOiJodHRwOi8vd3d
//I don't know what you mean by 10 - 30 since the example is 23 characters
var resultFromSubstring = environmentAgnosticPath.Substring(0,23);
var pattern = @"[A-Za-z0-9]{10,23}";
Regex regex = new Regex(pattern);
var resultFromRegex = regex.Match(environmentAgnosticPath).Value;
通常regex101是了解正则表达部分的好方法。