[英]Get number between characters in Regex
Having difficulty creating a regex. 创建正则表达式有困难。
I have this text: 我有这段文字:
"L\\":0.01690502,\\"C\\":0.01690502,\\"V\\":33.76590433" “ L \\”:0.01690502,\\“ C \\”:0.01690502,\\“ V \\”:33.76590433“
I need only the number after C\\": extracted, this is what I currently have. 我只需要C \\“之后的数字:提取,这是我当前拥有的。
var regex = new Regex(@"(?<=C\\"":)\d +.\d + (?=\s *,\\)");
var test = regex.Match(content).ToString();
decimal.TryParse(test, out decimal closingPrice);
To extract the number after C\\":
, you can capture (\\d+.\\d+)
in a group: 要提取
C\\":
之后的数字,可以在组中捕获(\\d+.\\d+)
:
You could also use a positive lookbehind: 您还可以在后面使用积极的表情:
You can use this code to fetch all pairs of letter and number. 您可以使用此代码来获取所有字母和数字对。
var regex = new Regex("(?<letter>[A-Z])[^:]+:(?<number>[^,\"]+)");
var input = "L\":0.01690502,\"C\":0.01690502,\"V\":33.76590433";
var matches = regex.Matches(input).Cast<Match>().ToArray();
foreach (var match in matches)
Console.WriteLine($"Letter: {match.Groups["letter"].Value}, number: {match.Groups["number"].Value}");
If you only need only number from "C" letter you can use this linq expression: 如果只需要“ C”字母中的数字,则可以使用此linq表达式:
var cNumber = matches.FirstOrDefault(m => m.Groups["letter"].Value == "C")?.Groups["number"].Value ?? "";
Regex explanation: 正则表达式说明:
(?<letter>[A-Z]) // capture single letter
[^:]+ // skip all chars until ':'
: // colon
(?<number>[^,"]+) // capture all until ',' or '"'
Fixed it with this. 对此进行了修复。
var regex = new Regex("(?<=C\\\":)\\d+.\\d+(?=\\s*,)");
var test = regex.Match(content).ToString();
String literal to use for C#: 用于C#的字符串文字:
@"C\\"":([.0-9]*),"
If you wish to filter for only a valid numbers: 如果您只想过滤有效数字:
@"C\\"":([0-9]+.[0-9]+),"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.