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 665025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:39:53+00:00 2026-05-13T23:39:53+00:00

When you are using a Regex instance, that is used in a method where

  • 0

When you are using a Regex instance, that is used in a method where that method is called a couple thousand times to parse things in a certain way, should that method include the Regex instance, or should the Regex instance be part of the class as a static member?

I get the feeling that initializing the same Regex thousands of times might be an overhead. But I am mainly concerned as best practice.

Where should I declare and define the Regex?

EDIT: Pseudo code:

static Regex regex ...

IEnumerable<string> Parse (string str)
{
    var matches = // use regex
    foreach (var match in matches)
    {
        ...
    }
}

void Main()
{
    foreach (var page in pages)
    {
         Parse (page); ...
    }
}
  • 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-13T23:39:53+00:00Added an answer on May 13, 2026 at 11:39 pm

    Your class that wraps your parsing functionality should contain a private reference (possibly static if it’s a static usage) to the regex, if you’re concerned about that kind of stuff.

    EDIT:

    To me, it’s not really about the performance, as since there’s the internal caching and all that jazz that Jimmy mentioned, I’d imagine the creation of a regex is probably not as expensive as the actual regex processing. It’s more about design principles: the factory method or parsing utility is conceptually operating using some internal filter (regex) to generate a list for you. If it’s the same one used over and over, that’s conceptually something you’re creating once and then keeping around and using over and over.

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

Sidebar

Related Questions

I am using regex in C# I need to parse a string till it
I'm using regEx and replace method to replace an empty space with a dash
I have a collection of strings that need to be parsed (using RegEx patterns)
I'm using a :regex library (found here ) to select elements that have IDs
Greetings All, I need to optimize a RegEx I am using to parse template
While using regex to help solve a problem in the Python Challenge , I
I have to validate using regex decimal number between 00.00 to 35.35 with following
I need some help using regex in java. Im trying to achieve 2 different
I tried writing a program in Java using regex to match a pattern and
I want to validate below data using regex and python. Below is the dump

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.