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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:10:22+00:00 2026-05-30T07:10:22+00:00

I have a collection of strings that need to be parsed (using RegEx patterns)

  • 0

I have a collection of strings that need to be parsed (using RegEx patterns) in the hopes of finding certain types of information. The “types” of information can be an email address or an ip address or a FQDN or whatever. The source string can have a single value (one email address), multiple values of the same type (for example, two ip addresses), a mix of values (an email address and an ip address), or nothing.

To represent a found pattern I have a single class that has properties for the type (email, ip, etc) and its value. Whatever method does the parsing should return a list of said class where the count can be zero, one, or more.

My question is does this type of scenario make sense for the Factory pattern? I cannot use a constructor where the string is passed in as a parameter since a constructor returns a single class instance.

I then though about the abstract Factory approach but form my reading Factories are designed to return different classes.

Then I read another StackOverflow question where somebody states that the static Create() method of the WebRequest class is a Factory pattern. So my thought is can I do that by passing in the source string?

Update: Based on this response (http://stackoverflow.com/a/4828511/240372) the factory pattern should be used when you have “distinct implementations of the same interface”. So my requirements do not meet that criteria. So…I am a little lost on the best approach…

Edit: I think my example use of email address and ip address may add confusion with people thinking I am only dealing with “addresses”. That is not the case. Let me add some pseudo code to help illustrate.

Class TypeClass
   Property Name As String
   Property Pattern As String
End Class

Class FoundValue
   Property TypeName As String
   Property Value As String
End Class

Dim possibleTypes as List(Of TypeClass)
possibleTypes.Add(New TypeClass() With {.Name = "Email", .Pattern = "some_regex_pattern" }
possibleTypes.Add(New TypeClass() With {.Name = "IPAddress", .Pattern = "some_regex_pattern" }
possibleTypes.Add(New TypeClass() With {.Name = "Date", .Pattern = "some_regex_pattern" }
possibleTypes.Add(New TypeClass() With {.Name = "Integer", .Pattern = "some_regex_pattern" }

Dim sourceStrings as List(Of String)
sourceStrings.Add("hello")
sourceStrings.Add("1.2.3.4")
sourceStrings.Add("someone@somewhere.com; who@what.com")
sourceStrings.Add("C:\Windows\notepad.exe 24 who@what.com")

For Each source in sourceStrings
    For Each type in possibleTypes
       ' compare type.pattern to source and return list of list of FoundTypes 
       '
       ' for example, the last source string would return:
       '  list containing
       '     New FoundValue() With { .TypeName = "Integer", .Value = "24" }
       '     New FoundValue() With { .TypeName = "Email", .Value = "who@what.com" }
       '
       '  whereas the second source would return
       '  list containing
       '     New FoundValue() With { .TypeName = "IPAddress", .Value = "1.2.3.4" }

Thank you.

  • 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-30T07:10:23+00:00Added an answer on May 30, 2026 at 7:10 am

    I dont think so-

    It sounds to me like you want to make a common interface for reading different types of “Things” from your text. I’d create concrete classes for each different thing type implementing the interface below.

    public interface IThingReader
    {
        IEnumerable<Thing> ReadThing(string content);
    }
    

    This approach allows you to encapsulate the reading/parsing/construction a specific type of address into a single place. In your case you may build a class to house a collection of readers and union the result of all of them. eg:

    public class CompositeReader
    {
        IThingReader[] Readers;
    
        public CompositeReader(IThingReader[] readers)
        {
           Readers = readers;
        }
    
        public List<Thing> ParseText(string text)
        {
            List<Thing> allThings = new List<Thing>();
    
               foreach(IThingReader reader in Readers)
               {
                   IEnumerable<Thing> things = reader.ReadThing(text);
                   allThings.AddRange(things);
               }
    
            return allThings;
        }
    }
    

    The factory would be more useful if you had to determine which type of thing was in a string and return the appropriate result. eg:

    public class EmailAddress : Thing
    public class IpAddress : Thing
    public class Number : Thing
    
    public class ThingFactory
    {
        public Thing GetThing(string text)
        {
            if (IsEmailAddress(text))
            {
                return new EmailAddress(text);
            }
            else if (IsIpAddress(text))
            {
                int[] ipAddressParts = SplitIpAddressParts(text);
                return new IpAddress(ipAddressParts);
            }
            else
            {
                throw new UnrecognisedThingException(text);
            }
        } 
    }
    

    So the example above uses a factory to determine which distinct implementation of the Thing class (common interface) to create.

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

Sidebar

Related Questions

I have a C# collection of strings. Each string is a sentence that can
I have a C# list collection that I'm trying to sort. The strings that
I have an SSIS package that does the following: Selects the connection strings from
I have an XML configuration file with a collection of strings like so: <SomeSetting>value</SomeSetting>
I have an application we bought that I need to integrate, and it uses
I need a way to convert a strings collection into a unique string. This
i have a GPS device that will be installed in many trucks. i can
I have a MS Access query ProductDetailsAll that is queried using an ASP.net web
I have a .NET web-service client that has been autogenerated from a wsdl-file using
Background Information: In the past, I have been picking up a collection of XML

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.