Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Named Capturing Group

Last post 05-20-2009, 2:32 AM by EvilMonkey. 5 replies.
Sort Posts: Previous Next
  •  05-19-2009, 7:51 AM 53148

    Named Capturing Group

    Hi,

    I would need to parse a file that has some delimited data...

    something like:

    [ACT1:Actor One ACT2: Actor Two ACT3: Actor Three] =  [ACT4:Actor Four ACT5: Actor Five ACT6: Actor Six]
    [ACT1:Actor One ACT2: Actor Two ACT3: Actor Three] =  [ACT4:Actor Four ACT5: Actor Five ACT6: Actor Six]
    [ACT1:Actor One ACT2: Actor Two ACT3: Actor Three] =  [ACT4:Actor Four ACT5: Actor Five ACT6: Actor Six]


    I would need to parse it line by line, if possible I would need to split each entry in

    ACT1=
    ACT2=
    ACT3=

    linked to

    ACT4=
    ACT5=
    ACT6=

     I have tried several stuff...but they all give me the whole line back at once and not the indivdual ACT's per line.

    Does anyone have any idea on how I can easily solve this with RegEx? Or do I need to parse it in 100% code?

     

    Thanks!!

     

  •  05-19-2009, 10:54 AM 53158 in reply to 53148

    Re: Named Capturing Group

    I'm not clear on what specific output you want or what platform you plan to use.
  •  05-19-2009, 10:58 AM 53159 in reply to 53148

    Re: Named Capturing Group

    You question is unclear.

    As each line is your sample data is exactly the same, it gives me the impression that this is a made up sample and not your real data.  We expressly ask in the posting guidelines that you do not  make up samples.  If this were real data and each line is the same you can simple hard code your result since there is no variation in the association

    The guidelines also ask for other information which you have also failed to provide.  This does not seem to be a regex only task in any event but with insufficent information who can tell?


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  05-19-2009, 5:09 PM 53174 in reply to 53148

    Re: Named Capturing Group

    Okay...I'm sorry... I did provide limited (no) information...

    The platform is .net.

    What I would like to do is to parse a file that contains multiple lines formatted in a specific way.
    Basically it's a column mapping between 2 databases.

    The file contains the following data. (these are just a couple of lines, the file is over 300 lines long)

    [DB:Master_DB Table:Customer Column:FirstName]=[DB:crm_db Table:_Customers Column:CustFirstName]
    [DB:Master_DB Table:Customer Column:LastName]=[DB:crm_db Table:_Customers Column:CustLastName]
    [DB:Master_DB Table:Customer Column:Address1]=[DB:crm_db Table:_Customers Column:AddressLine1]

    So the first line means that the column Master_DB.Customer.FirstName is linked to crm_db._Customers.CustFirstName.

    I need to create an application in C# to visualise and edit these links... a grid containing the Source Tables and Columns (the Left side of the '='). Whenever the user selects a row(a column), I want to show the Column mapped to the selected column (the Right side of the '=').

    I need to be able to delete a mapping or change a mapping.

    So basically I would need to do the following things:

     - Read all mappings: If possible broken out by DB,Table or Column.
         for example: DB:Master_DB
                            Table:Customer
                            Column:FirstName
                            DB:crm_db
                            Table:_Customers
                            Column:CustFirstName
    - Replace an existing mapping:
       for example [DB:Master_DB Table:Customer Column:FirstName]=[DB:crm_db Table:_Customers Column:CustFirstName] has to be replaced by [DB:Master_DB Table:Customer Column:FirstName]=[DB:crm_db2 Table:_Customers Column:CustFirstName]

    - Delete an existing mapping

     

    Did I provid information? Can I do this with the help of regular expressions? 

     

    Any help is greatly appreciated!!

  •  05-19-2009, 5:39 PM 53177 in reply to 53174

    Re: Named Capturing Group

    C#.NET Code Example:
    using System;
    using System.Text.RegularExpressions;
    namespace myapp
    {
        class Class1
       
    {
            static void Main(string[] args)
            {
                String sourcestring = "[DB:Master_DB Table:Customer Column:FirstName]=[DB:crm_db Table:_Customers Column:CustFirstName]";
                Regex re = new Regex(@"\[DB:([^]]*?) Table:([^]]*?) Column:([^]]*)]=\[DB:([^]]*?) Table:([^]]*?) Column:([^]]*)]");
                MatchCollection mc = re.Matches(sourcestring);
                int mIdx = 0;
                foreach (Match m in mc)
                {
                    for (int gIdx = 0; gIdx < m.Groups.Count; gIdx++)
                    {
                        Console.WriteLine("[{0}][{1}] = {2}", mIdx, re.GetGroupNames()[gIdx], m.Groups[gIdx].Value);
                    }
                    Console.WriteLine("{0}.{1}.{2} is linked to {3}.{4}.{5}", m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value, m.Groups[4].Value, m.Groups[5].Value, m.Groups[6].Value);
                    mIdx++;
                }
            }
        }
    }


  •  05-20-2009, 2:32 AM 53185 in reply to 53177

    Re: Named Capturing Group

    THANKS A LOT!!! This works fantastic!!!
View as RSS news feed in XML