Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
I have a text file with mixed string and integer like this:
X12 Y0.4 Z8
X4.5 Y231 Z32.4
Y0.4 Z8
X54 Y5.11 Z9
Y78 Z68
.
.
.


I have only X,Y,Z in text file
I Always have Y,Z but may not have X
I defined double x,y,z in my program,
how i can read integers in each line and save their values as x,y,z?
Should I use the pattern? how i can write a pattern for this text file?
please help me
Posted
Updated 17-Feb-13 21:37pm
v6
Comments
Saurabh_Damle 18-Feb-13 3:14am    
Will all the lines contain X,Y and Z values only or it will contain more such patterns.
e.g.
1. X12Y0.4Z8 - Only X,Y and Z
2. X12Y0.4Z8A24 - In this case along with X,Y,Z we have A.
fika_fa 18-Feb-13 3:30am    
I modify my question
only X,Y,Z
Always have Y,Z but may not have X
like this:
X12 Y0.4 Z8
X4.5 Y231 Z32.4
Y0.4 Z8
X54 Y5.11 Z9
Y78 Z68

Personally, I would use a regex:
(?<key>[a-zA-Z]+)(?<value>(\d*\.\d+)|(\d+))
Should do it.


[edit]Encoded HTML - damnit! - OriginalGriff[/edit]


Long update from OP

You could, but it's pretty horrible to maintain.
Did you know that you can get more than one Match from a regex?
Try this:
C#
Regex regex = new Regex(@"(?<key>[a-zA-Z]+)(?<value>(\d*\.\d+)|(\d+))");
float x = 0.0F, y = x, z = x;
string[] lines = File.ReadAllLines(pathToFile);
foreach (string line in lines)
    {
    foreach (Match m in regex.Matches(line))
        {
        string key = m.Groups["key"].Value;
        string val = m.Groups["value"].Value;
        float f;
        if (float.TryParse(val, out f))
            {
            switch (key)
                {
                case "X": x = f; break;
                case "Y": y = f; break;
                case "Z": z = f; break;
                }
            }
        }
    Console.Write("{0},{1},{2}", x, y, z);
    }
 
Share this answer
 
v3
Comments
fika_fa 18-Feb-13 3:35am    
I modify my question
please read again
OriginalGriff 18-Feb-13 4:08am    
Yes - just run the Regex on each line, and it will break it into Matches. Each Match with have a Key ("X", "Y", or "Z") and a Value (the number associated with the particular Key)
Simples!
fika_fa 19-Feb-13 5:45am    
thanks for your reply
how I can use it in code?
Can I use the following regex?
((X(\d*\.\d+)|(\d*))\s(Y(\d*\.\d+)|(\d*))\s(Z(\d*\.\d+)|(\d*)))
|((Y(\d*\.\d+)|(\d*))\s(Z(\d*\.\d+)|(\d*)))

Is the code that I wrote is true?

Regex regex = new Regex(?<key>[a-zA-Z]+)(?<value>(\d*\.\d+)|(\d+));

string[] lines = File.ReadAllLines(pathToFile);
float x,y,z;
foreach (string line in lines)
{
Match match = regex.Match(line);
if (match.Success)
{
string key = match.Groups[1].Value;
string value = match.Groups[2].Value;
if(key=="X")
x=value;
else if(key=="Y")
y=value;
else if(key=="Z")
z=value;
}
}
OriginalGriff 19-Feb-13 6:14am    
Answer updated
Jegan Thiyagesan 18-Feb-13 5:32am    
Good one!
Do you have pattern like X12 Y0.4 Z8, in this case I can see a space character in between. If that is the case then you can just split the string with space character. As you said X may or may not be present and that can be determined on the basis of number of items the resultant array holds. You can then get the respective value by replacing X,Y or Z accordingly.

If space is not present in between...
In this case also you can check if X exists in the line and use substring to get numeric part.
 
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