简体   繁体   English

我正在尝试使用正则表达式从RichTextBox检索文本

[英]I'm trying to use a regular expression to retrieve text from RichTextBox

As the header is implying I'm trying to retrieve a certain line of numbers from a richtextbox and then put it into a separate textbox. 正如标题所暗示的那样,我正在尝试从富文本框中检索某些数字行,然后将其放入单独的文本框中。 I've tried this code below but it doesn't wanna work with me. 我已经在下面尝试过此代码,但它不想与我一起使用。 It's probably way wrong and there's probably easier ways of doing it but I'm quite new to this stuff and I would appreciate all the help I can get in this matter. 这可能是错误的方式,并且可能有更简单的方法,但是对于这件事我还是很陌生,我将不胜感激。

I have a textbox called tbPersNr; 我有一个名为tbPersNr的文本框;

A RichTextBox called tbText; 一个称为tbText的RichTextBox;

A button which is called btnGet; 一个名为btnGet的按钮;

    string regPattern = @"\\d{6}-\\d{4}";

    int indexOfSearch = 0;

    private void btnGet_Click(object sender, EventArgs e)
    {
        int startIndex = 0;

        if (tbText.Text.Length > 0)
        {
            startIndex = HittaPersNr(regPattern, startIndex, tbText.Text.Length); 
        }

        if (startIndex > 0)
        {
            this.tbPersNr.Text = regPattern.ToString();
        }
    }

    public int HittaPersNr(string txtToSearch, int searchStart, int searchEnd)
    {
        // Setting default value to -1.
        int retVal = -1;

        // Validating start of the search
        // om indexOfSearch = -1, slutar sökningen
        if (searchStart >= 0 && indexOfSearch >= 0)
        {
            // Validating end of search
            if (searchEnd > searchStart || searchEnd == -1)
            {
                // Searching for results in richtextbox
                indexOfSearch = tbText.Find(regPattern, searchStart, searchEnd, RichTextBoxFinds.None);

                // Validating if search resulted in any finds.
                if (indexOfSearch != -1)
                {
                    // putting index to value in the text.
                    retVal = indexOfSearch;
                }
            }
        }
        return retVal;
    }

UPDATE 更新

Cheers for all helpful answers,,,and sorry for expressing myself quite clumsy. 为所有有用的答案而欢呼,并对表示自己很笨拙感到抱歉。 But I expected I was on the wrong track here and I'm gonna try fix my app with some of the answers. 但是我希望自己在这里走错了路,我将尝试通过一些答案修复我的应用。 Welbog,,,good detective work there,,,hehehe,,,good that you understood what I was meaning. 韦尔博格,那里很好的侦探工作,嘿嘿,好,你明白我的意思了。 Thanks all of you who pointed out how to write the Regex in a proper way,,,quite confusing. 感谢所有指出如何以适当方式编写Regex的人,这很令人困惑。

Most greatful 最伟大的

Simon 西蒙

RichTextBox.Find() is not a RegEx-Function! RichTextBox.Find() 不是 RegEx函数!

Use Regex.Match(tbText.Text, regPattern) instead. 请改用Regex.Match(tbText.Text, regPattern)

Unless you really want to search for something that contains \\ , I think you might have too many backslashes there. 除非您真的想搜索包含\\ ,否则我认为您可能在其中包含过多的反斜杠。 Shouldn't it be this? 不是吗

    string regPattern = @"\d{6}-\d{4}";

The @ in front of your string prevents the backslashes from being interpreted, so they become part of the string. 字符串前面的@阻止反斜杠被解释,因此它们成为字符串的一部分。

If you are trying to use a regular expression that checks for 6 digits, followed by a - and then 4 digits, then you regex is wrong: 如果您尝试使用正则表达式来检查6位数字,然后依次检查-和4位数字,则您的正则表达式是错误的:

@"\\d{6}-\\d{4}"

You either use a string literal , or escape your \\ but not both. 您可以使用字符串文字 ,或转义\\但不能同时使用两者。

Either of these is OK: 这些都可以:

@"\d{6}-\d{4}"
"\\d{6}-\\d{4}"

Additionally, you are trying to use a regular expression in the Find method of the RichTextbox , which doesn't take a regular expression! 另外,您尝试在RichTextboxFind方法中使用正则表达式,而该方法不使用正则表达式!

It looks like you intend to match your pattern and then print the result to the textbox named tbPersNr . 看起来您打算匹配您的模式,然后将结果打印到名为tbPersNr的文本框中。 At least that's what I think this code is for: 至少这就是我认为此代码的用途:

if (tbText.Text.Length > 0)
{
  startIndex = HittaPersNr(regPattern, startIndex, tbText.Text.Length);
}

if (startIndex > 0)
{
  this.tbPersNr.Text = regPattern.ToString();
}

But regPattern is a string set to \\\\d{6}-\\\\d{4} . 但是regPattern是设置为\\\\d{6}-\\\\d{4}的字符串。 It is not replaced with what the pattern matched, which I believe is what you're expecting (I can't really tell, but I see no reason why you would print a string literal like that). 它不会被匹配的模式代替,我相信这是您所期望的(我不能真正告诉您,但是我看不出您为什么要打印这样的字符串文字)。 So in addition to the points the other answers bring up, you need to actually retrieve the value you've matched from your rich text box in order to display it. 因此,除了其他答案提出的要点之外,您还需要从富文本框中实际检索出匹配的值才能显示出来。

A call to Substring will do the trick, using the startIndex you've already found and a length of 11 (which is the length of a string matched by your regular expression). 调用Substring可以解决这个问题,它使用您已经找到的startIndex11的长度(这是与您的正则表达式匹配的字符串的长度)。

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

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