我想在一张材料说明书中找到并替换毫米到厘米。我必须找到int数旁边的单词mm,然后将mm改为cm,并将这个数字乘以0.1倍,问题是描述可能有所不同,例如:
我有下面的正则表达式来查找文本,但是它并不像预期的那样工作,有时在查找150 0mm的0毫米时是不起作用的:
string txt = textBox1.Text;
string re1 = ".*?"; // Non-greedy match on filler
string re2 = "\\d+"; // Uninteresting: int
string re3 = ".*?"; // Non-greedy match on filler
string re4 = "\\d+"; // Uninteresting: int
string re5 = ".*?"; // Non-greedy match on filler
string re6 = "\\d+"; // Uninteresting: int
string re7 = ".*?"; // Non-greedy match on filler
string re8 = "(\\d+)"; // Integer Number 1
string re9 = ".*?"; // Non-greedy match on filler
string re10 = "(mm)"; // Word 1
// ".*?\\d+.*?\\d+.*?\\d+.*?(\\d+).*?(mm)"
Regex r = new Regex(re1 + re2 + re3 + re4 + re5 + re6 + re7 + re8 + re9 + re10,
RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m = r.Match(txt);
if (m.Success)
{
String int1 = m.Groups[1].ToString();
String word1 = m.Groups[2].ToString();
MessageBox.Show("(" + int1.ToString() + ")"
+ "(" + word1.ToString() + ")" + "\n");
}那么.你有什么想法吗?也许一个更复杂的正则表达式或者一个库来查找和替换..。谢谢你!!
发布于 2016-05-07 16:14:42
下面是一个关于艾德龙的工作程序
string s = @"Half plate ¼ inch length 188mm height 1065mm, ss 316 Lace 3/4 cal 16 x 1120 mm ss 304
Air coushion rode 3/16 38mm width, 972mm length ss316
Vacuum plate L.972mm W.288mm ss304";
Regex regex = new Regex(@"(\d+)(\s*)(mm)");
string ns = regex.Replace(s, delegate (Match m) {
return Int32.Parse(m.Groups[1].Value) * 0.1 + m.Groups[2].Value + "cm";
});
Console.WriteLine(ns);产出如下:
Half plate ¼ inch length 18.8cm height 106.5cm, ss 316 Lace 3/4 cal 16 x 112 cm ss 304
Air coushion rode 3/16 3.8cm width, 97.2cm length ss316
Vacuum plate L.97.2cm W.28.8cm ss304https://stackoverflow.com/questions/37089978
复制相似问题