Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all, i have text file like this

LINE 1: {'DEPT-ELECTRICAL';'USER';
LINE 2: 'This is the first performance for DMM.'; 'TIME:12:00:00'; 'VERSION:6.1.2
LINE 3: ';'F01RY0DM08';'#history';'UP8";

I need to consider and read the contents with in single quotes as a single string. For eg., the output should be String1 = DEPT-ELECTRICAL, String2 = USER...
Semi colon is the delimiter here.

I have referred some solutions online. I copied some code for a head start eventhough it's not exactly what i require but it's giving me an error as "foreach statement cannot be applied to variables of type Systems.Text.Regularexpr.match". please help me

What I have tried:

C#
// Read all text from file
                string sData = File.ReadAllText(@"c:/file.txt");

                // Match strings between " "
                Match match = Regex.Match(sData, "\"(\\w|\\d|\\s|\\\")*\"",
                                          RegexOptions.IgnoreCase);

                // Read results and strip " out of them
                foreach (var sResult in match)
                {
                    sResult = sResult.Remove(0, 1).Remove(sResult.length - 2, 1);
                    // Do whatever with sResult
                }
Posted
Updated 10-Nov-16 22:24pm

A match is only a single result. You are looking for the collection result, "Matches"

C#
MatchCollection matches = Regex.Matches(...
foreach (Match match in matches) {
   string sResult = match.Value
   ...
 
Share this answer
 
v3
Comments
Member 12226114 11-Nov-16 4:00am    
I am getting 'Missing assembly reference error' now. But I included 'using System.Text.RegularExpressions;' statement already
Midi_Mick 11-Nov-16 4:06am    
Apologies. The Matches declaration should be a MatchCollection. Fixing answer
I fear your RegEx do not match what you looking for.
Try something like: "'(.+)';"

Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 

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