Click here to Skip to main content
15,890,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text file that contains several words
...........
...........  dimension="13.7mm" row="30" speed="13.4" ........


I want to extract the value 13.7mm
This is what I have done 
//loaded the file
string file = "c:\\temp\\myTextFile.txt";
string searchText = "dimension=";
//Declare reader as a new StreamReader with file as the file to use  

    StreamReader reader = StreamReader(file);  

    
//Declare text as the reader reading to the end  

     String text = reader.ReadToEnd();  

     //If the searchText is a match  

     if (Regex.IsMatch(text, searchText))  

     {
        
         MessageBox.Show("found it");

       //OK now that I found the word 'dimension="
       //how to display the next characters between the next "  and " 
       //the number of characters may vary (e.g. 125.7mm)
            
    }


What I have tried:

tried to split it
sring[] lines = Regex.Split("server=", " ");

         foreach (string line in lines)
         {
             listBox1.Items.Add(line);
            
         }
Posted
Updated 19-Oct-17 11:39am
Comments
j snooze 19-Oct-17 17:40pm    
is there more than 1 dimension= in the text file and you have to find all of them? otherwise you should be able to get the starting position of the "dimension=" and then get the next 2 double quote positions starting from the dimension= using indexof and substr methods.
Karthik_Mahalingam 19-Oct-17 23:25pm    
post few lines of the textfile.

1 solution

Use a capture in your regular expression. See here for code sample: Regex Groups - Dot Net Perls[^]
 
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