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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:40:28+00:00 2026-05-26T22:40:28+00:00

Trying to match this data, that is contained in one string: vsan 1 interfaces:

  • 0

Trying to match this data, that is contained in one string:

vsan 1 interfaces:
fc2/1             
vsan 10 interfaces:
fc1/1             fc1/2             fc1/3             fc1/4             
fc1/5             fc1/6             fc1/7             fc1/8             
fc1/9             fc1/10            fc1/11            fc1/12            
fc1/13            fc1/14            fc1/15            fc1/16    

The output I am getting is correctly grouped by each vsan, but I am only getting the first interface (fcnn/nn) in each. For instance, in vsan 10 I want all the interfaces but I am only getting fc1/1.
Here is the Regex I am using:

string MemberMatchString = 
    @"vsan\s(?<number>\d+)[\s]interfaces:\n\s+(?<interfaces>\sfc\d+/\d+)\s+\n?";
MatchCollection MemberList = Regex.Matches(block, MemberMatchString);
  • 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-26T22:40:29+00:00Added an answer on May 26, 2026 at 10:40 pm

    it tink the best solution would be a Tokenizer/Parser to get the Information you want.

    I don’t think you can handle it (efficient and transparent for others) with a single Regex. In my opinion a Regex is not always the Solution for complex string problems.

    I also think, that a large Regex is not maintainable at any time and should therefore be avoided. (Try it by yourself, get any complex Regex from any Page from the Internet. Don’t read, what the expression should do, and try describing the functionality of the expression. You would be supprised what the writer expected, and what you expect the expression to do.

    Simple Regex to bold my point:

    What does this Regex do?

    (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
    

    This Regex is the implementation of the RFC 2822 from this page.

    And this one is a reduced one from the same page.

    [a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
    

    Both can be used to check a e-mail address! Did you expect this?

    Solution:

    Create a Tokenzier/Parser: So to show you how i would solve this problem I prepared a working sample for you.
    Just Copy paste it to a Console Application, should work fine 🙂
    Please give me feedback about my code and if my helps was usefull for you.

    Sample explanation:

    Create a Tokenizer, which splits the expressions into matching tokens. In this case the Tokens derive from Interface IToken.
    The Tokens are created by Line and than by a regex Match with groups. See method Tokenize and TokenizeLine. They do the hard work.

    1. Splitting (Line and Spaces)
    2. Matching (if Vsan or Interface)

    After Splitting it up it gets real simple. Just move forward through the Tokens and create the VSan and Interface Objects.
    The Tokens are order after the order of the String Groups (defined by Regex in Tokenize) so moving through the list is always:

    VSan
       Interface
       Interface
       Interface
    
    Vsan
       Interface
       Interface
    

    So you can easy create a VSan Object and adding all Interfaces afterwards. If the next VSan Token is in line just create a new VsanObject an continue with adding Interfaces to the new Instance.

    Now you have a list of Vsans which contains a List of Interfaces which you can use .
    I overwrote the ToString Method to make the result visible.

    Sample Code:

    public static class Program{
    
            static string VsanString = 
    @"vsan 1 interfaces:
    fc2/1             
    vsan 10 interfaces:
    fc1/1             fc1/2             fc1/3             fc1/4             
    fc1/5             fc1/6             fc1/7             fc1/8             
    fc1/9             fc1/10            fc1/11            fc1/12            
    fc1/13            fc1/14            fc1/15            fc1/16    ";
    
            public static void Main (string[] args) {
    
                VSanTokenizer vSanTokenizer = new VSanTokenizer(VsanString);
                IList<IToken> tokens = vSanTokenizer.Tokens;
                VsanTokenInterpreter vsanTokenInterpreter = new VsanTokenInterpreter(tokens);
                IList<IVSan> vSans = vsanTokenInterpreter.VSans;
                foreach (IVSan vSan in vSans) {
                    Console.WriteLine(vSan.ToString());
                }
                Console.WriteLine("Please press return to quit.");
                Console.ReadLine();
            }
        }
    
        interface IVSan {
            int Number { get; }
            IList<IInterface> Interfaces { get; }
        }
    
        class VSan : IVSan {
            private readonly IList<IInterface> interfaces;
            private readonly int number;
    
            public VSan(int number, IList<IInterface> interfaces) {
                this.number = number;
                this.interfaces = interfaces;
            }
    
            public override string  ToString() {
    
                StringBuilder toString = new StringBuilder();
    
                toString.Append("Vsan with Number: ");
                toString.Append(number);
                toString.Append(" has following Interfaces:");
                toString.AppendLine("");
    
                foreach (IInterface @interface in Interfaces) {
                    toString.Append("Intefaces with Name: ");
                    toString.Append(@interface.Name);
                    toString.AppendLine("");
                }
                return toString.ToString();
            }
    
            #region Implementation of IVSan
    
            public int Number {
                get { return number; }
            }
    
            public IList<IInterface> Interfaces {
                get { return interfaces; }
            }
    
            #endregion
        }
    
    
        interface IInterface {
            string Name { get; }
        }
    
        class Interface : IInterface {
            private readonly string name;
    
            public Interface(string name) {
                this.name = name;
            }
    
            #region Implementation of IInterface
    
            public string Name {
                get { return name; }
            }
    
            #endregion
        }
    
        interface IToken {
            string Value { get; }
        }
    
        interface IVsanToken : IToken {
            int VsanInterfaceNumber { get; }
        }
    
        internal abstract class AbstractToken : IToken {
            private readonly string value;
    
            public AbstractToken(string value) {
                this.value = value;
            }
    
            #region Implementation of IToken
    
            public string Value {
                get { return value; }
            }
    
            #endregion
        }
    
        class VsanToken : AbstractToken, IVsanToken {
    
            private readonly int vsanInterfaceNumber;
    
            public VsanToken(string value)
                : base(value) {
                vsanInterfaceNumber = int.Parse(value);
            }
    
            #region Implementation of IVsanToken
    
            public int VsanInterfaceNumber {
                get { return vsanInterfaceNumber; }
            }
    
            #endregion
        }
    
        class InterfaceToken : AbstractToken, IInterfaceToken {
    
            private readonly int firstNumber;
            private readonly int secondNumber;
    
            public InterfaceToken(string value)
                : base(value) {
    
                Match match = Regex.Match(value, "fc([0-9])/([0-9]+)");
                Group firstNumberGroup = match.Groups[1];
                Group secondNumberGroup = match.Groups[2];
    
                firstNumber = int.Parse(firstNumberGroup.Value);
                secondNumber = int.Parse(secondNumberGroup.Value);
            }
    
            public int SecondNumber {
                get { return secondNumber; }
            }
    
            public int FirstNumber {
                get { return firstNumber; }
            }
        }
    
        interface IInterfaceToken : IToken {
            //Edited: Added Second and FirstNumber to Interface so it can be accessed
            int SecondNumber { get; }
    
            int FirstNumber { get ; }
    
        }
    
        class VSanTokenizer {
            private readonly string vSanString;
            private IList<IToken> tokens;
    
            public VSanTokenizer(string vSanString) {
                this.vSanString = vSanString;
                tokens = Tokenize(vSanString);
            }
    
            public string VSanString {
                get { return vSanString; }
            }
    
            private IList<IToken> Tokenize(string vSanString) {
                List<IToken> tokens = new List<IToken>();
                StringReader reader = new StringReader(vSanString);
                string readLine = reader.ReadLine();
                while (readLine != null) {
                    IList<IToken> tokenizeLine = TokenizeLine(readLine);
                    tokens.AddRange(tokenizeLine);
                    readLine = reader.ReadLine();
                }
                return tokens;
            }
    
            private IList<IToken> TokenizeLine(string readLine) {
                IList<IToken> tokens = new List<IToken>();
                Match vsanInterfaceDeclartion = Regex.Match(readLine, "vsan ([0-9]+) interfaces:");
                if (vsanInterfaceDeclartion.Success) {
                    Group numberGroup = vsanInterfaceDeclartion.Groups[1];
                    VsanToken vsanToken = new VsanToken(numberGroup.Value);
                    tokens.Add(vsanToken);
                    return tokens;
                }
    
                Match vsanInterface = Regex.Match(readLine, "(fc[0-9]/[0-9]+)");
                if (vsanInterface.Success) {
                    GroupCollection groupCollection = vsanInterface.Groups;
                    foreach (Group vsanInterfaceGroup in groupCollection) {
                        string value = vsanInterfaceGroup.Value;
                        IToken token = new InterfaceToken(value);
                        tokens.Add(token);
                    }
                }
    
                return tokens;
            }
    
            public IList<IToken> Tokens {
                get {
                    return tokens;
                }
            }
        }
    
        class VsanTokenInterpreter {
            private readonly IList<IToken> tokens;
            private readonly IList<IVSan> vSans;
    
            public VsanTokenInterpreter(IList<IToken> tokens) {
                this.tokens = tokens;
    
                this.vSans = ParseTokens(tokens);
            }
    
            private IList<IVSan> ParseTokens(IList<IToken> tokens) {
                IList<IVSan> vsans = new List<IVSan>();
    
                IVSan currentVSan = null;
                foreach (IToken token in tokens) {
                    if (token is IVsanToken) {
                        currentVSan = CreateVSan((IVsanToken)token);
                        vsans.Add(currentVSan);
                    } else if (token is IInterfaceToken) {
                        if (currentVSan == null)
                            throw new Exception("First Vsan Line has to be declared!");
                        IInterface inter = CreateInterface((IInterfaceToken)token);
    
                        currentVSan.Interfaces.Add(inter);
                    }
                }
    
                return vsans;
            }
    
            protected virtual IInterface CreateInterface(IInterfaceToken interfaceToken) {
                //Edited: you can now access the First/Second Number from the Interface Token and use it to create the Instance of your interface:
    
                int firstNumber = interfaceToken.FirstNumber;
                int secondNumber = interfaceToken.SecondNumber;
                return new Interface(interfaceToken.Value);
    
            }
    
            protected virtual IVSan CreateVSan(IVsanToken vsanToken) {
                return new VSan(vsanToken.VsanInterfaceNumber, new List<IInterface>());
    
            }
    
            public IList<IVSan> VSans {
                get { return vSans; }
            }
    
            public IList<IToken> Tokens {
                get { return tokens; }
            }
        }
    

    Result and Conclusion:

    As a result you get a IVsan which contains the IInterfaces. You can use this Model in your code to get Information you looking for.

    1. By using a Object Model you can change the parsing at any time,
      without changing anything in your business logic.
    2. The Parsing logic is more simple and understandable than in Regex
    3. There are readable Exception possible (for user Input)
    4. You can resolve int/bools a.s.o. during transformation
    5. Your colleagues can handle and maintain your code.

    Please ask me if you have furhter questions, i would be glad helping you! Please also give me feedback, I’m open for new impressions!

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

Sidebar

Related Questions

first, this is using preg. String I'm trying to match: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa b c d
I have a string such as: #sometag-{serialized-data-here} And I want to match this pattern
I have a data that looks similar to this Tables contains more than one
i'm trying to find a way to match this problem but i'm LOST i
I am trying to do this regex match and replace but not able to
I get this error: System.Reflection.TargetException: Object does not match target type. when trying to
I'm trying to match elements with a name that is 'container1$container2$chkChecked' , using a
When trying to add a few items to the database I'm getting this error:
I'm trying to create a Listbox that displays text and an image as one
I'm following this guide: http://www.math.umd.edu/~dcarrera/ruby/0.3/chp_01/programs.html and I'm trying to create my first ruby program.

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.