开发者

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

开发者 https://www.devze.com 2022-12-24 22:04 出处:网络
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.

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;

A RichTextBox called tbText;

A button which is called 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开发者_Go百科, 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.

Most greatful

Simon


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:

@"\\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!


RichTextBox.Find() is not a RegEx-Function!

Use Regex.Match(tbText.Text, regPattern) instead.


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.


It looks like you intend to match your pattern and then print the result to the textbox named 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}. 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).

0

精彩评论

暂无评论...
验证码 换一张
取 消