Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Matching string (populated via an embedded resouce) with Regex to populate a hashtable with three columns

Posted on 2006-11-28
7
Medium Priority
?
290 Views
Last Modified: 2010-04-16
Hi,

I want to populate a hashtable with three columns (Isin, company-name, share-description) which I will use for textual and phonetic comparisons with data in a dataset. I know the syntax is incorrect, but am unsure what the Regex syntax particularly the foreach( Match match in      Regex.Matches( result,
                        @"(<?:isin>\W+),(<?:company-name>\W+),(<?:share-description>\W+)\n"))...

any help greatly appreciated.

The code:

public class Values
            {
            }

            public void TestVoid()
            {
                  string result = GetResource ( "Digita.Importers.CoTax.PerTax.ISINlist.csv" ); //this reads the embedded resource fine


                  System.Collections.Hashtable table = new System.Collections.Hashtable();
                                               
                                                //this is one of the problems - no matches are made
                  foreach( Match match in      Regex.Matches( result,
                        @"(<?:isin>\W+),(<?:company-name>\W+),(<?:share-description>\W+)\n"))
                  {
                        string isin = match.Groups[ "isin" ].ToString();
        string company = match.Groups[ "company-name" ].ToString();
                        string share = match.Groups[ "share-description" ].ToString();
                        
                                          //this also causes an error
                        table[ isin ] = new Values( isin, company-name, share-description );
                  }
}
0
Comment
Question by:JakeyCakes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 18029363
Hi JakeyCakes;

What is the format of this string, result, which is the results of reading in the resource file? This is needed to know how to set up the Regex object correctly.

Fernando
0
 

Author Comment

by:JakeyCakes
ID: 18029464
Fernando,

the string format is :

"TE0123456789,TEST Ltd.,TEST SHARE1\r\nTEN0123456789,Tested N.V.,Test share2\r\n"

HTH
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 18030160
Hi JakeyCakes;

Try it this way.

      public void TestVoid()
      {
            string result = GetResource ( "Digita.Importers.CoTax.PerTax.ISINlist.csv" ); //this reads the embedded resource fine
            MatchCollection mc = Regex.Matches(result,
                        @"(?<isin>[^,]+),(?<company>[^,]+),(?<share>(?:[^\r]+))");

            System.Collections.Hashtable table = new System.Collections.Hashtable();
                                               
            //this is one of the problems - no matches are made
            foreach( Match m in mc)
            {
                  string isin = m.Groups["isin"].Value;
                  string company = m.Groups["company"].Value;
                  string share = m.Groups["share"].Value;
                   
                  //this also causes an error
                  table[ isin ] = new Values( isin, company, share );
            }
      }


Fernando
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:JakeyCakes
ID: 18035254
Fernando,

this line of code -  table[ isin ] = new Values( isin, company, share ); - causes this error : No overload for method 'Values' takes '3' arguments. How can I resolve this?
0
 

Author Comment

by:JakeyCakes
ID: 18036263
Fernando I think I have resolved the above by doing:

table[isin] = isin;
table[company] =company;
table[share] = share;

I now have a related regex problem (and I know strictly speaking I should ask a new question, but as its related ... ):

in the DataRow row in the populated DataSet a, I want to use the Hashtable for comparision purposes, in otherwords if any value in the table[company]  case-insensitively matches the value in the row.ItemArray[2].ToString() (the company name), i want the specified strings populated as follows:

Company = the value of table[company],
strISIN = the value of table[isin],
Description = the value of table[share]

I have tried

foreach ( DataRow row in a.Tables[0].Rows )
                        {
                        if ( Regex.IsMatch(table[company].ToString(),quoteReplace ( row.ItemArray[2].ToString() ),RegexOptions.IgnoreCase))
                                                      {
                                                            Company =  table[company].ToString()                                                             strISIN =  table[isin].ToString();
                                                            Description = table[share].ToString()                                                       }
                                                      else
                                                      {
                              Company = row.ItemArray[2].ToString()
                                                      }

to no avail. Do you have any suggestions?
0
 

Author Comment

by:JakeyCakes
ID: 18036841
Fernando,

I have decided that it wasn't fair of me to ask you to tackle a new issue - which I have posted as a new question - when you actually had resolved my original problem, so the points are yours.

Thank your very much for your assistance.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 18036913
Hi JakeyCakes;

Regex.IsMatch(table[company].ToString(),quoteReplace ( row.ItemArray[2].ToString() ),RegexOptions.IgnoreCase))

Where:
1       table[company].ToString() Is the string to be searched.
2       quoteReplace ( row.ItemArray[2].ToString() ) Is the string to match

Item 2 above may cause a no match to be found or a invalid match to be found. This will happen if the string that holds the match pattern, item 2 above, has any of the following characters. ==>  . $ ^ {  [ ( | ) * + ? \  <==. These characters have a special meaning in the Regex language and therefore needs to be escaped before using them. To escape the characters in the string do one of the following:

If using VS 2005

        string escapedStr = Regex.Escape(quoteReplace ( row.ItemArray[2].ToString() ))
        if ( Regex.IsMatch(table[company].ToString(), escapedStr, RegexOptions.IgnoreCase))


If using VS before 2005

        string escape = @"(\.|\^|\[|\{|\(|\||\)|\*|\+|\?|\\)";
        string escapedStr = Regex.Replace(quoteReplace ( row.ItemArray[2].ToString() ), escape, "\$1");
        if ( Regex.IsMatch(table[company].ToString(), escapedStr, RegexOptions.IgnoreCase))

That is the only thing that I can see that would cause it not to match correctly.

Fernando
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question