Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am asking for a help regarding pattern matching and changing to its equivalent structure. Actually, I don't know how to explain it well or how should I call the name of my problem but I will give you the example:

My input for example is:
Prefix[v1],[v2],[v3],[v4]

Then the equivalent patter will be search based on the prefix, for example the equivalent of the pattern is:

output[v2] is var1=[v1],var2=[v3],var3=[var4]

So this would be the situation:

input: EX12,test%23,54

pattern: EX[v1],[v2]%[v3],[v4]
searched output pattern: Code_[v2] is v1=[v1],v2=[v3],v3=[v4]

output: Code_test is v1=12,v2=23,v3=54

I hope my example is enough to explain what I want to do... Kindly someone please give me an idea or a link if this was already discussed on how should I make an array index becomes variable in position like in
((Type)Controls.Find("string", true)[0])
because I think that is my problem.

The arrangement of array variables and its length are unknown and same as the delimiter that will be used. My cue is only the [v#] pattern (or any better idea that can be suggested). Or if at least those variable pattern [v#] can be directly converted to string array to achieve my result.
Thank you.


Best regards,
Raymond
Posted
Updated 21-Nov-13 5:26am
v3
Comments
Member 9440960 21-Nov-13 23:09pm    
I already got the idea of regex and have an idea on how to do the function.
Thank you very much because it gives me an idea on how to solve my problem.
I can use the replace method of regex in a loop to replace the string content by using the "[vn]" as my keyword.
But lastly, I am in a speed critical function. Kindly tell me if my idea of using regex repeatedly is faster than my created function?
Thanks.

Assuming your values are numeric, then an Regex can do that:
(?<=[^\d]+)(\d+)(?:[^\d]+)(\d+)(?:[^\d]+)(\d+)

The three numeric values woudl be extarcted as strings into the Groups collection by a simple match:
C#
Regex findValues = new Regex(@"(?<=[^\d]+)(\d+)(?:[^\d]+)(\d+)(?:[^\d]+)(\d+)");
Match m = findValues.Match("EX12,text%23,54");
for (int i = 0; i < m.Groups.Count; i++)
    {
    Console.WriteLine(m.Groups[i].Value);
    }
Group[0] would be fairly useless to you, but the others would be what you wanted.
 
Share this answer
 
Comments
BillWoodruff 21-Nov-13 19:57pm    
Wow ! +5. Mind-reader.
Thank you very much got your response.

I like the regex idea, but unfortunately I don't even know if what type of value I would get, I created a sample function below which is somehow is similar to what I want to achieve but the code is a bit long and the positions of the data array is fixed...

About regex, I need to study that because I never used its function before.

C#
    //Pattern Holder
	public class PatternVar
    {
        public string strPrefix;
        public string strTable;
        public string bInsert;
        public string bReply;
        public string strPattern;
    }
	//Input data Holder
    public class InputExtract
    {
        public string dbTable;
        public int patternNumber;
        public string[] dbData;
    }



        public InputExtract SearchInputPattern(string inputPattern,List<patternvar> pVar,char delim)
        {
            InputExtract extractedData = new InputExtract();
            extractedData.patternNumber = -1;
            string sPrefix = inputPattern.Substring(0, 3);
			//pVar contains the list of prefix with its equivalent pattern
            foreach (PatternVar x in pVar)
            {
                if (x.strPrefix == sPrefix)
                {
					//if prefix match is found, return the List index together with the array of input data
                    extractedData.dbTable = x.strTable;
                    extractedData.dbData = inputPattern.Remove(0,3).Split(delim);
                    extractedData.patternNumber = pVar.IndexOf(x);
                    break;
                }
            }

            return extractedData;
        }
        public string DecodePattern(InputExtract InputExt, List<patternvar> pVar,char delim)
        {
            string DBCommand = "";
            if (InputExt.patternNumber != -1)
            {
			//create a pattern accordin to the inputs parameter
                if ((pVar[InputExt.patternNumber].bInsert == "Y")||(pVar[InputExt.patternNumber].bInsert == "y"))
                {
                    DBCommand = "INSERT INTO " + InputExt.dbTable + " VALUES(";
                    for (int x = 0; x < InputExt.dbData.Length; x++)
                    {
                        DBCommand += InputExt.dbData[x] + ",";
                    }
                    DBCommand = DBCommand.Remove(DBCommand.Length - 1, 1);
                    DBCommand += ");";
                }
                else
                {
                    string[] Headers = pVar[InputExt.patternNumber].strPattern.Split(delim);
                    DBCommand = "UPDATE " + InputExt.dbTable + " SET ";
                    for (int x = 1; x < InputExt.dbData.Length; x++)
                    {
                        DBCommand += Headers[x] + InputExt.dbData[x] + ",";
                    }
                    DBCommand = DBCommand.Remove(DBCommand.Length - 1, 1);
                    DBCommand += " WHERE " + Headers[0] + InputExt.dbData[0] + ";";
                }
            }
            return DBCommand;
        }
</patternvar></patternvar>


What I need is a simple few lines of command like your example... but I don't know how should it be done.

My idea is to create a function like:

string result = PatternMatch(string inputData, List<matchtable> mTable)

where
MatchTable{
string InputPattern;
string EquivalentPattern;
}

Any further suggestions?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900