string str = "var rawItemInfo={\"stock\":\"In stock. Limit 5 per customer.\",\"shipping\":\"$19.99 Shipping\",\"finalPrice\":\"139.99\",\"itemInfo\":\"<div class=\"grpPricing\"";
var regPattern = "stock\":\"(.*?)\",\".*?shipping\":\"(.*?)\",\".*?finalPrice\":\"(.*?)\"";
Regex regex = new Regex(regPattern);
var data = regex.Match(str);
string val1 = data.Groups[1].Value.ToString(); // In stock. Limit 5 per customer.
string val2 = data.Groups[2].Value.ToString(); // $19.99 Shipping
string val3 = data.Groups[3].Value.ToString(); // 139.99
如何编辑正则表达式以从运费中提取19.99
?目前,它正在给我“$19.99 Shipping
”。任何帮助将不胜感激。
记住!运送也可以这样"shipping\":\"free Shipping\"
答案 0 :(得分:2)
var regPattern = "stock\":\"(.*?)\",\".*?shipping\":\"(?:(?:\\$([0-9]*\\.[0-9]*)|(free Shipping)).*?)\",\".*?finalPrice\":\"(.*?)\"";
string val1 = data.Groups[1].Value.ToString(); // In stock. Limit 5 per customer.
string val2 = data.Groups[2].Value.ToString(); // 19.99
string val3 = data.Groups[3].Value.ToString(); // free Shipping
string val4 = data.Groups[4].Value.ToString(); // 139.99
只有val2和val3中的一个将被赋值(另一个将是String.Empty)
如果您的送货始终是123.456(因此在。之后总会有一些数字。和之后的一些数字。)
答案 1 :(得分:0)
在切割和其他一些技术困难(即缺乏咖啡因 - 延长“二十三感觉”)方面遇到了一些麻烦。以下是解决方案的全部内容:
using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main()
{
String s = "var rawItemInfo={\"stock\":\"In stock. Limit 5 per customer.\",\"shipping\":\"$19.99 Shipping\",\"finalPrice\":\"139.99\",\"itemInfo\":\"<div class=\"grpPricing\"";
Console.WriteLine("Testing '{0}'", s);
test(s);
Console.WriteLine();
s = "var rawItemInfo={\"stock\":\"In stock. Limit 5 per customer.\",\"shipping\":\"free Shipping\",\"finalPrice\":\"139.99\",\"itemInfo\":\"<div class=\"grpPricing\"";
Console.WriteLine("Testing '{0}'", s);
test(s);
}
public static void test(String s)
{
String p = "stock\":\"(.*?)\",\".*?shipping\":\"(.*?)\",\".*?finalPrice\":\"(.*?)\"";
// modification
p = "stock\":\"(.*?)\",\".*?shipping\":\"(\\$([\\d\\.]+)|(free))\\s?(.*?)\",\".*?finalPrice\":\"(.*?)\"";
Regex regex = new Regex(p);
var data = regex.Match(s);
for (Int32 i = 0; i < data.Groups.Count; i++)
Console.WriteLine("{0}. {1}", i, data.Groups[i].Value);
Console.WriteLine("Price is {0}", data.Groups[2].Value.Equals("free")?"FREE":data.Groups[3].Value);
}
}
使用的实际模式:
"stock\":\"(.*?)\",\".*?shipping\":\"(\\$([\\d\\.]+)|(free))\\s?(.*?)\",\".*?finalPrice\":\"(.*?)\""
结果排队:
Group # Sample Data Notes
0 (whole string) [both]
1 In stock. Limit 5 per customer. [both]
2 free/$19.99 [free/with price]
3. 19.99 [populated only for price]
4. free [populated only for free]
5. Shipping [both]
6. 139.99 [both]
以上代码的示例输出:
Testing 'var rawItemInfo={"stock":"In stock. Limit 5 per customer.","shipping":"$19.99 Shipping","finalPrice":"139.99","itemInfo":"<div class="grpPricing"'
0. stock":"In stock. Limit 5 per customer.","shipping":"$19.99 Shipping","finalPrice":"139.99"
1. In stock. Limit 5 per customer.
2. $19.99
3. 19.99
4.
5. Shipping
6. 139.99
Price is 19.99
Testing 'var rawItemInfo={"stock":"In stock. Limit 5 per customer.","shipping":"free Shipping","finalPrice":"139.99","itemInfo":"<div class="grpPricing"'
0. stock":"In stock. Limit 5 per customer.","shipping":"free Shipping","finalPrice":"139.99"
1. In stock. Limit 5 per customer.
2. free
3.
4. free
5. Shipping
6. 139.99
Price is FREE
答案 2 :(得分:0)
您只需要在其中的正确位置\$?
:
var regPattern = "stock\":\"(.*?)\",\".*?shipping\":\"\\$?(.*?) .*?\",\".*?finalPrice\":\"(.*?)\"";
答案 3 :(得分:0)
通用解决方案怎么样?
string str = String.Concat(
"var rawItemInfo={",
string.Join(",", "\"stock\":\"In stock. Limit 5 per customer.\"",
"\"shipping\":\"$19.99 Shipping\"",
"\"finalPrice\":\"139.99\"",
"\"itemInfo\":\"<div class=\\\"grpPricing\\\" />\""),
"}");
// recognize a JSON* object
var item = @"""(?<key>[^""]+)""\s*:\s*""(?<value>(?:[^\\""]|\\"")+)""";
var jsonRe = new Regex(@"\{\s*" + item + @"(?:\s*,\s*" + item + @")*\s*\}", RegexOptions.Compiled);
var m = jsonRe.Match(str);
// build the corresponding dictionary
var json = new System.Collections.Specialized.StringDictionary();
if (m.Success)
{
int captures = m.Groups["key"].Captures.Count;
for (int i = 0; i < captures; i++)
{
json[m.Groups["key"].Captures[i].Value] = m.Groups["value"].Captures[i].Value;
}
}
// get the values
var val1 = json["stock"]; // "In stock. Limit 5 per customer."
var val2 = json["shipping"]; // "$19.99 Shipping"
var val3 = json["finalPrice"]; // "139.99"
// extract price from val2
val2 = Regex.Replace(val2, @"(?:\$(\d+\.\d{2})|(free)) Shipping", @"$1"); // "19.99"
* p.s.,“JSON”是正确的术语吗?