简体   繁体   English

字符串中的正则表达式

[英]Regex in String

I am trying to write a keylogger, with an intent of just getting the float numbers from keyboard input and with them compute some formula and display to the user.我正在尝试编写一个键盘记录器,目的是从键盘输入中获取浮点数,并用它们计算一些公式并显示给用户。 My code so far (the HookCallBack part)到目前为止我的代码(HookCallBack 部分)

 private static IntPtr HookCallback(
            int nCode, IntPtr wParam, IntPtr lParam)
        {

            string xxx = "";
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
            {
                int vkCode = Marshal.ReadInt32(lParam);   


                if (((Keys)vkCode).ToString() == "OemPeriod")
                {
                    //Console.Out.Write(".");
                    xxx += ".";
                    xxx.Replace("OemPeriod", ".");
                }
                if (((Keys)vkCode).ToString() == "D1")
                {
                    //Console.Out.Write(".");
                    xxx += "1";
                    xxx.Replace("D1", "1");
                }
                if (((Keys)vkCode).ToString() == "D2")
                {
                    //Console.Out.Write(".");
                    xxx += "2";
                    xxx.Replace("D2", "2");
                }

all the way up to 9 and 0一直到 9 和 0

                }
                if (((Keys)vkCode).ToString() == "Space")
                {
                    //Console.Out.Write(".");
                    xxx += " ";
                    xxx.Replace("Space", " ");
                }


            }


            string test = "2.3 4.5 6.7";   

            Regex regex = new Regex(@"\d+\.\d+");
            var ma2 = regex.Matches(xxx);
            Console.Out.Write(ma2.Count);

            xxx = String.Empty;      

            return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }

My issue is this: im not able to make the regex to work (ma2.Count = 0) with the string xxx (which is the keyboard input from the user), but when i replace xxx with the string test, it works.我的问题是:我无法使正则表达式与字符串 xxx(这是用户的键盘输入)一起工作(ma2.Count = 0),但是当我用字符串测试替换 xxx 时,它可以工作。 What im doing wrong.我做错了什么。 Dont know if i was clear enough.不知道我是否足够清楚。 Newbie here.新手来了Thks in advance!提前谢谢!

Updated:更新:

It looks like this method is being called every time a new key is being called, however, you are updating a local variable called xxx .看起来每次调用新键时都会调用此方法,但是,您正在更新一个名为xxx的局部变量。

Every time this method gets invoked, you are always starting with an empty string.每次调用此方法时,您总是从一个空字符串开始。 At most, xxx will have 1 character. xxx最多有 1 个字符。

You will need store the value of xxx outside the scope of the function.您需要将xxx的值存储在 function 的 scope 之外。

Not recommended (because you want to avoid static variables for this type of thing), but you could introduce a new variable:不推荐(因为您想避免static变量用于此类事物),但您可以引入一个新变量:

private static string InputFields = "";

and use InputFields += ".";并使用InputFields += "."; for example.例如。

Also note that xxx.Replace(...) doesn't actually do anything here because you are not assigning the result of this method to anything.另请注意, xxx.Replace(...)在这里实际上没有做任何事情,因为您没有将此方法的结果分配给任何东西。 You can drop it altogether.你可以完全放弃它。


As per @CinCout's comment, are you able to verify the value of xxx ?根据@CinCout 的评论,您是否能够验证xxx的值?

I tried the example above and it should work with the test input:我尝试了上面的示例,它应该与test输入一起使用:

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        var test = "2.3 4.5 6.7";
        var regex = new Regex(@"\d+\.\d+");
        var matches = regex.Matches(test);

        foreach (var match in matches)
        {
            Console.WriteLine(match);
        }
    }
}

Demo演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM