正则表达式语句问题

时间:2016-05-03 22:31:15

标签: c# regex if-statement

第一篇帖子所以如果我没有提供足够的信息,我很抱歉,不过,我会尽力而为。此外,可能明智的是,我对此知之甚少,我基本上知道这个代码的作用,就是这样。

简而言之,我是会员或模拟游戏社区,主要包括围绕从警察跑步巡航。游戏记录所有聊天消息,因为它们被直接发送到.txt文件,此代码在聊天记录中搜索下面代码中列出的参数之一,如果匹配一个,它将播放相应的" beep"

前两个工作然而第三个我有一些挣扎,并想知道这里是否有人可以帮助我理解。这些"警察"的功能之一有一个速度枪,它在游戏中显示结果如下:

user clocked @ 105kph/ 65mph• ignored *

user clocked @ 138kph/ 86mph• **

*这低于速度限制

**这高于速度限制

然而在聊天记录中他们看起来像这样:

user ^Lclocked @ 138kph/ 86mph•

user ^Lclocked @ 63kph/ 39mph•^L ignored

我尝试过使用各种通配符(假设它们有效)并以这两种方式编写代码:

Regex expspeeding = new Regex(@"mph• ", RegexOptions.IgnoreCase);
Regex expspeeding = new Regex(@"#mph•^L ignored ", RegexOptions.IgnoreCase);

但它根本不起作用。

以下是我所拥有的主要内容,如果有人甚至可以建议进入该方向,那将非常感谢。

如有任何问题,我会尽力回答。

感谢您的时间。

{
public partial class Form1 : Form
{
    LogFileTracer tracer;

    // The search patterns
    Regex expdc = new Regex(@"Disconnected", RegexOptions.IgnoreCase);
    Regex expidle = new Regex(@"You will be kicked soon", RegexOptions.IgnoreCase);
    Regex expnospeeding = new Regex(@"?• ignored", RegexOptions.IgnoreCase);


    public Form1()
    {
        InitializeComponent();


        tracer = new LogFileTracer();
        tracer.onTextReceived += Tracer_onTextReceived;
    }

    // Event if line was read from logfile
    private void Tracer_onTextReceived(string text)
    {
        try
        {
            if (InvokeRequired)
            {
                // Thanks Microsoft for that threading bullcrap
                this.Invoke(new LogFileTracer.TextReceivedDelegate(Tracer_onTextReceived), text);
                return;
            }
        }
        catch
        {
            // Nobody cares
        }

        if (text == null || text.Length == 0) return;

        // Check whether any of the patterns match
        if (expdc.IsMatch(text))
        {
            // play sound pattern
            txtEventLog.AppendText(text + "\r\n");
            Console.Beep(1000, 100);
            Console.Beep(750, 100);
            Console.Beep(500, 100);

        }
        else if (expidle.IsMatch(text))
        {
            // play sound pattern
            txtEventLog.AppendText(text + "\r\n");
            Console.Beep(2000, 50);
            Console.Beep(2050, 50);
            Console.Beep(2000, 50);

        }
        else if (expnospeeding.IsMatch(text))
        {
            // play sound pattern
            txtEventLog.AppendText(text + "\r\n");
            Console.Beep(1000, 50);
            Console.Beep(2050, 50);
            Console.Beep(2000, 50);

        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        LblStatus.Text = "Logger Status: " + (tracer.isAlive ? "running" : "dead");

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Try to start the reader with the textbox
        if (!string.IsNullOrEmpty(TxtFilename.Text)) tracer.start(TxtFilename.Text);
    }

    private void CmdStart_Click(object sender, EventArgs e)
    {
        // Start or restart the tracer
        tracer.start(TxtFilename.Text);
    }

}

}

2 个答案:

答案 0 :(得分:0)

您可以对mph•$进行正则表达式检查。这匹配以mph•结尾的所有行,但不包含包含ignored - 关键字的行。

答案 1 :(得分:0)

描述

如果您正在尝试解析以下文本

user ^Lclocked @ 138kph/ 86mph•
user ^Lclocked @ 63kph/ 39mph•^L ignored

然后我会使用这样的正则表达式:

^user\s+\^Lclocked\s+@\s+([0-9]+)kph/\s*([0-9]+)mph[^\n\r]*

正则表达式将捕获数据并将它们放入捕获组中,如下所示:

  • 组0获取完整字符串
  • 第1组获取kph值
  • 第2组获得英里/小时值

注意:我使用标志表示多行和不区分大小写。

样本匹配

[0][0] = user ^Lclocked @ 138kph/ 86mph•
[0][1] = 138
[0][2] = 86

[1][0] = user ^Lclocked @ 63kph/ 39mph•^L ignored
[1][1] = 63
[1][2] = 39

解释

Regular expression visualization

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  user                     'user'
----------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  \^                       '^'
----------------------------------------------------------------------
  Lclocked                 'Lclocked'
----------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  @                        '@'
----------------------------------------------------------------------
  \s+                      whitespace (\n, \r, \t, \f, and " ") (1 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    [0-9]+                   any character of: '0' to '9' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  kph/                     'kph/'
----------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    [0-9]+                   any character of: '0' to '9' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  mph                      'mph'
----------------------------------------------------------------------
  [^\n\r]*                 any character except: '\n' (newline), '\r'
                           (carriage return) (0 or more times
                           (matching the most amount possible))