名称userInput在当前上下文中不存在为什么?

问题描述

| 我现在有这个:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1

static void Main(string[] args)
{
    Console.WriteLine(\"Which teams have played? right vs inbetween the two teams\");
    Match m = Regex.Match(\"^(?<team1>\\.+) vs (?<team2>\\.+)$\",userInput); 
    if (m.Success) 
    {     
        string team1 = m.Groups[\"team1\"].Value;
        string team2 = m.Groups[\"team2\"].Value; 
    } 

    Console.WriteLine(\"What is the score? \");    
    Match m = Regex.Match(\"^(?<score1>\\.+)/(?<score2>\\.+)$\",userInput);
    if (m.Success) 
    {
        int score1 = m.Groups [\"score1\"].Value;
        int score2 = m.Groups [\"score2\"].Value;
    }
我在点上收到一个错误,说:   \“无法识别的转义序列
^(?<team1>\\.+)
\” 它也确实在
score1
score2
行中告诉我:   \“找不到类型或名称空间名称
Match
(您是否缺少using指令或笨拙的引用?)\” 和:   \“名称Regex在当前上下文中不存在。 我想做的是从一行中读取两个答案,并将其读取为两个答案。 例如:BlueTeam vs RedTeam。 也想为分数做同样的事情。例如:1/1我知道斜线有点奇怪,但是我只是出于懒惰而想到其他东西。 然后在路径上,我想将其分配到xy表中。像这样:http://pastebin.com/ahgP04bU     

解决方法

        通过将变量放在ѭ5中,您将使用这些变量创建单独的块作用域。它们不在该范围之内。 不要那样做。
Regex
类位于
System.Text.RegularExpressions
名称空间中;您需要使用
using
语句包含该名称空间。     ,        您还必须包括
using System.Text.RegularExpressions
为了在不完全限定的情况下使用正则表达式。 编辑:还有很多可以改善您的代码。当您在
if
块内定义变量时,这些变量将在局部范围内分配给if块,并在if块结束时立即进行垃圾回收。 另外,我看不到在代码的任何位置定义了userInput的位置。     ,         您需要确定名称空间的范围,大括号(
{ }
)当前缺失。 文件顶部需要need12 need。
userInput
不存在,您需要声明:
var userInput = string.Empty;
如果字符串中包含特殊字符(例如
\\
),则可以使用
@
@\"\\myString\"