Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6634001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:52:44+00:00 2026-05-25T22:52:44+00:00

I am writing some code that uses fixed regexs to search strings and pattern

  • 0

I am writing some code that uses fixed regexs to search strings and pattern match.

Its simple stuff, but I want to improve regex performance with compiling (its a high traffic website).

I was thinking of compiling the regex and putting it in a Shared (static) variable inside a class.

Something like this:

Namespace Regexs

    Public Class UrlNickname

        Private Shared rgx As Regex = New Regex("^\/\w{4,20}$", RegexOptions.IgnoreCase Or RegexOptions.CultureInvariant Or RegexOptions.Compiled)

        ''' <summary>
        ''' Returns a Nickname string if pattern found in Url, otherwise returns Empty string.
        ''' </summary>
        ''' <param name="url">The Url string to search.</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function ContainsNickname(url As String) As String
            If rgx.IsMatch(url) Then
                Return url.Substring(1, url.Length - 1)
            End If
            Return String.Empty
        End Function

    End Class

End Namespace

Then you could use the function like this:

Dim url As String = HttpContext.Current.Request.RawUrl

Dim nickname As String = Regexs.UrlNickname.ContainsNickname(url)
If Not String.IsNullOrEmpty(nickname) Then
    //nickname pattern match found:
    //do something like RedirectToRoutePermanent
End If

Basically , I store the regex in a Shared (static) variable so that is only compiled once.

The function would then be called to check if a username pattern match was found on a 404 error page.

Whould this be the best approach for improving regex performance?

Note: I am not interested in a solution for 404 error page problems above, its just a simple example.


Further advances could then use a Shared generic list of regexs , like so:

Private Shared _rgxList As List(Of Regex)

Public Sub New()
    //get list of regex expressions from database and populate:
    _rgxList.Add(New Regex("blah", RegexOptions.Compiled))
    _rgxList.Add(New Regex("blah2", RegexOptions.Compiled))

Public Shared Function IsMatch(str) as Boolean
    With each reg in _rgxList
        return reg.IsMatch(str)

New() would be called on Application.Start event.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:52:44+00:00Added an answer on May 25, 2026 at 10:52 pm

    Looks fine. In addition, I’d make the Shared variable ReadOnly, to avoid accidental changes.

    Note, however, that compiling regular expressions increases performance by about 30% at runtime, but it also has its downsides, in particular, it requires more memory. A nice comparison can be found here:

    • Coding Horror: To Compile or Not To Compile

    So, there’s no general “A is better than B” answer here. It all depends on your exact requirements. You might have to do measurements to find out what performs better in your particular case.

    General advice on performance tuning: Make sure that something is really the performance bottleneck before improving it. It doesn’t matter if your regex takes 0.01 or 0.02 seconds, if, for example, looking up the nickname in the databsae takes 2 seconds. Use built-in tools from the .net Framework (e.g. Stopwatch Class) or external tools (e.g. EQATEC Profiler) to find out where your bottlenecks are.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing some code in VB.NET that uses a switch statement but in
I'm writing some code that uses dynamic shared libraries as plugins. My command line
I'm writing some code that uses some unmanaged calls into user32 functions such as
I'm writing some python code to interact with a C DLL that uses structures
I'm writing some code on a mobile device that uses a REST service to
I'm writing some code that uses a Tree (a regular tree that can have
I am writing some JavaScript code that uses a string rendered with PHP. How
I've been writing WinForms code that uses a BackgroundWorker to do some async work,
I'm writing Cocoa code that uses some classes defined in a C++ library. I
I am writing some code that uses MPI and I was keeping noticing some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.