Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查找出现多次的特定字符串后面的数字_C#_Regex_Numbers - Fatal编程技术网

C# 查找出现多次的特定字符串后面的数字

C# 查找出现多次的特定字符串后面的数字,c#,regex,numbers,C#,Regex,Numbers,我已经在这周围寻找了一段时间了,我似乎只是成功地混淆了我自己,所以任何人能给予的任何帮助都将是惊人的 现在我有一个文本文件,它相当大,超过10万行 文本文件如下所示: The apple is set at Price: £1.00 Sale: £3.50 Price: £2.00 Plum reduced to Sale: £2.00 Bananas are usually Price: £4.00 Price: £3.00 Price: £2.00 等等 现在我想提取所有的数字,只提取

我已经在这周围寻找了一段时间了,我似乎只是成功地混淆了我自己,所以任何人能给予的任何帮助都将是惊人的

现在我有一个文本文件,它相当大,超过10万行

文本文件如下所示:

The apple is set at Price: £1.00
Sale:  £3.50
Price: £2.00
Plum reduced to Sale:  £2.00
Bananas are usually Price: £4.00
Price: £3.00
Price: £2.00
等等

现在我想提取所有的数字,只提取字符串“Price:”后面的数字(no),目前只需在控制台中打印出来即可

预期产出应为:

1.00
2.00
4.00
3.00
2.00
There were 100,000 lines.
我有以下几点,虽然我确信它离我们有一百万英里远

int counter = 0;
string line;
string input1 = " Price: £";
string price;

// Read the file and display it line by line.  
System.IO.StreamReader file =
    new System.IO.StreamReader(@"C:Pricelist.txt"); 
while ((line = file.ReadLine()) != null)
{
    price = Regex.Match(input1, @"\d+").Value;
    System.Console.WriteLine(price);
    //System.Console.WriteLine(line);
    counter++;
}

file.Close();
System.Console.WriteLine("There were {0} lines.", counter);
// Suspend the screen.  
System.Console.ReadLine();
我的想法是正则表达式查找input1字符串,然后找到下一个数字,但它似乎不起作用。我是否需要让它读取line变量中的字符串集,或者这是一个坏主意


再说一次,我有点迷路了,所以任何指点都很好。如果需要更多信息,请询问:)

尝试以下正则表达式:
Price:(\d+\.\d+)
,Price将位于第一个捕获的组中

说明:

  • 价格:£
    -带所需前缀的文字
  • (\d+\.\d+)
    -捕获带小数部分的组匹配价格

尝试以下正则表达式:
Price:(\d+\.\d+)
,Price将位于第一个捕获的组中

说明:

  • 价格:£
    -带所需前缀的文字
  • (\d+\.\d+)
    -捕获带小数部分的组匹配价格

以下正则表达式应该执行您想要的操作:

@"(?<=Price: £).*"

以下正则表达式应该执行您想要的操作:

@"(?<=Price: £).*"

考虑到你说的

目前,只需在控制台中打印出来即可

我将价格变量存储在
var valueList=new List()
中,这样您就可以使用
valueList.ForEach(value=>Console.WriteLine(value))允许您在以后的任何阶段使用这些值(如果需要)

至于自己提取价格:

var prices = line.Split(' ');
var valueList = new List<string>();
prices.ToList().ForEach(p => {
   if (p.StartsWith("£"))
   valueList.Add(p.Substring(1));
   });
var价格=行分割(“”);
var valueList=新列表();
prices.ToList().ForEach(p=>{
如果(p.开始,以“;”号填列)
增加(p.子字符串(1));
});

以前建议的Regex选项较短,但有些人不喜欢使用Regex,所以这里有一个不使用Regex的解决方案。

考虑到您的说法

目前,只需在控制台中打印出来即可

我将价格变量存储在
var valueList=new List()
中,这样您就可以使用
valueList.ForEach(value=>Console.WriteLine(value))允许您在以后的任何阶段使用这些值(如果需要)

至于自己提取价格:

var prices = line.Split(' ');
var valueList = new List<string>();
prices.ToList().ForEach(p => {
   if (p.StartsWith("£"))
   valueList.Add(p.Substring(1));
   });
var价格=行分割(“”);
var valueList=新列表();
prices.ToList().ForEach(p=>{
如果(p.开始,以“;”号填列)
增加(p.子字符串(1));
});

以前建议的Regex选项较短,但有些人不喜欢使用Regex,因此这里有一个不使用Regex的解决方案。

您的原始代码从不使用
变量。这是必须匹配的-而不是
input1

此外,正则表达式可以在循环外部定义一次,并在循环内部重复调用。静态
Regex
方法每次调用时都会创建一个新的
Regex
实例。这意味着在循环中调用静态
Regex.Replace()
方法100000次将创建100000个
Regex
实例

int counter = 0;
string line;
string price;
var regex = new Regex("Price: £(?<amount>.*)");

// Read the file and display it line by line.  
using (System.IO.StreamReader file = new System.IO.StreamReader(@"c:Pricelist.txt"))
{
  while ((line = file.ReadLine()) != null)
  {
    var match = regex.Match(line);
    if (match.Success)
    {
      price = match.Groups["amount"].Value;
      System.Console.WriteLine(price);
    }
    //System.Console.WriteLine(line);
    counter++;
  }
}

System.Console.WriteLine("There were {0} lines.", counter);
// Suspend the screen.  
System.Console.ReadLine();
int计数器=0;
弦线;
串价;
var regex=新regex(“价格:£(?.*);
//读取文件并逐行显示。
使用(System.IO.StreamReader file=new System.IO.StreamReader(@“c:Pricelist.txt”))
{
而((line=file.ReadLine())!=null)
{
var match=regex.match(行);
如果(匹配成功)
{
价格=匹配。组[“金额”]。值;
系统控制台写入线(价格);
}
//系统控制台写入线(行);
计数器++;
}
}
System.Console.WriteLine(“有{0}行。”,计数器);
//暂停屏幕。
System.Console.ReadLine();

原始代码从不使用
变量。这是必须匹配的-而不是
input1

此外,正则表达式可以在循环外部定义一次,并在循环内部重复调用。静态
Regex
方法每次调用时都会创建一个新的
Regex
实例。这意味着在循环中调用静态
Regex.Replace()
方法100000次将创建100000个
Regex
实例

int counter = 0;
string line;
string price;
var regex = new Regex("Price: £(?<amount>.*)");

// Read the file and display it line by line.  
using (System.IO.StreamReader file = new System.IO.StreamReader(@"c:Pricelist.txt"))
{
  while ((line = file.ReadLine()) != null)
  {
    var match = regex.Match(line);
    if (match.Success)
    {
      price = match.Groups["amount"].Value;
      System.Console.WriteLine(price);
    }
    //System.Console.WriteLine(line);
    counter++;
  }
}

System.Console.WriteLine("There were {0} lines.", counter);
// Suspend the screen.  
System.Console.ReadLine();
int计数器=0;
弦线;
串价;
var regex=新regex(“价格:£(?.*);
//读取文件并逐行显示。
使用(System.IO.StreamReader file=new System.IO.StreamReader(@“c:Pricelist.txt”))
{
而((line=file.ReadLine())!=null)
{
var match=regex.match(行);
如果(匹配成功)
{
价格=匹配。组[“金额”]。值;
系统控制台写入线(价格);
}
//系统控制台写入线(行);
计数器++;
}
}
System.Console.WriteLine(“有{0}行。”,计数器);
//暂停屏幕。
System.Console.ReadLine();

您可以在一行上有多个价格吗?有没有没有没有价格的线?这些价格总是浮动的吗?另外,您似乎在
new System.IO.StreamReader(@“C:\Pricelist.txt”)
中遗漏了一个
\
。每行只有一个价格。可以有空行。它们总是浮动的。然后检查下面的答案。你能在一行上有多个价格吗?有没有没有没有价格的线?这些价格总是浮动的吗?另外,您似乎在
new System.IO.StreamReader(@“C:\Pricelist.txt”)
中遗漏了一个
\
。每行只有一个价格。可以有空行。它们总是浮动的。然后检查下面的答案。