S
S
Slavik Sharov2015-09-04 10:19:15
Regular Expressions
Slavik Sharov, 2015-09-04 10:19:15

What could be the error when replacing in a string?

private string guidToName(string src)
        {
            Regex reg1 = new Regex("cad[0-9a-f]{5}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}");
            Regex reg2 = new Regex("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}");
            string str = src;
            while (true)
            {
                if (reg1.IsMatch(str))
                {
                    string oldStr=reg1.Match(str).ToString();
                    string newStr =session.GetAttributeType(new Guid(reg1.Match(str).ToString())).Name;
                    str.Replace(oldStr, newStr);
                }
                else if (reg2.IsMatch(str))
                    str.Replace(reg2.Match(str).ToString(), session.GetObject(new Guid(reg2.Match(str).ToString())).Caption);
                else break;
            }
            return str;
        }

at the input - a line that can contain guide codes
at the output - a line with guides replaced with headers
and everything seems to be fine, and it enters the loop, and fulfills the first condition, when it comes to replay, here is some kind of garbage, in theory it should replace guide to the name and move on, but the line contained the guide, so it sits there, and it turns out an endless loop

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
heartdevil, 2015-09-04
@spark36

Hello.
By the Replace method

This method does not change the value of the current instance. Instead, a new string is returned with all occurrences of oldValue replaced with newValue.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question