Results 1 to 15 of 15

Thread: is Replace() faster than Regex.replace() ?

Threaded View

  1. #1

    Thread Starter
    Frenzied Member wengang's Avatar
    Join Date
    Mar 2000
    Location
    Beijing, China
    Posts
    1,604

    is Replace() faster than Regex.replace() ?

    hi all.
    I've been implementing regular expressions throughout my project this week, and I've been surprised by how much faster it processes string tasks.

    But this time I found one where Replace is working much faster.

    I have an array of 5000 different characters that will be replaced by 5000 other characters.

    The loop looks like:
    VB Code:
    1. For t = 1 To 5000
    2.             'strText = Replace(strText, Old(t), New(t))
    3.             'strText = strText.Replace(Old(t), New(t))
    4.             strText = Regex.Replace(strText, Old(t), New(t))
    5.   Next
    With the first replace, it's a bit slow (1 minute for a 1mb document)
    The second one does the same file in about 20 seconds
    but with the Regex.replace, after more than 1 minute, I stop the app and it is on t=240 or so.
    Why?
    everywhere else I've tried, the regex.replace was faster.
    Could it be the character encoding?

    Second question....
    I am trying to do a regex.replace (different one) where the find string may span across more than one line of text
    so I tried:
    sText = Regex.Replace(sText, "<ut .+?ut>", "", RegexOptions.Multiline)
    but that does not do it.
    I tried:
    sText = Replace(sText, Chr(10), "")
    sText = Replace(sText, Chr(13), "")
    sText = Regex.Replace(sText, "<ut .+?ut>", "")
    and that does work. But I read that regex could search and replace across lines, and I assumed this parameter was it.
    How can I get this to replace when the target Text looks like this:
    <ut sdfdssd>fdsfsdfsdfsdfsdfsdfsdfsdf
    </ut>dsfsdfsdfsdfsd

    ?

    Thanks
    Wengang
    Last edited by wengang; Jun 5th, 2005 at 09:53 PM.
    Wen Gang, Programmer
    VB6, QB, HTML, ASP, VBScript, Visual C++, Java

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width