Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a string in which i want to fetch number before TTL using regex how can i do that
this is my string
C#
WPNCB«                                                         
19AUG DEPARTURE DATE-----LAST DAY TO PURCHASE 21MAY/2359       
       BASE FARE      EQUIV AMT      TAXES             TOTAL   
 1-       DKK790      USD139.00     179.40XT       USD318.40ADT
    XT    109.80YQ      19.20YR      29.00ZO       5.40UA      
            2.00UD      14.00YK                                
             790         139.00     179.40            318.40TTL // here is TTL
ADT-01  EPX NPX                                                
 CPH SU X/MOW SU HRK78.68EPX SU X/MOW SU CPH59.45NPX NUC138.13 
 END ROE5.71905                                                
VALID ON SU/FARE RESTR APL                                     
VALIDATING CARRIER - SU

<pre lang="c#">
    i used \d{1,4}\.\d{2}TTL regex but it fetch TTL to i dont want to show TTL line.

Thanks in advance
Posted
Comments
Sergey Alexandrovich Kryukov 14-May-13 10:02am    
With Regex, it should be to fixed characters or patterns delimiting the substring you need. In your case, to simplify the answer, what are they? It should be ended with "TTL" (not including), but what's the starting pattern?
—SA

i just made some changes in my regex and it works

C#
(?:\b)([0-9]{0,}[.][0-9]{2})(?=TTL)
 
Share this answer
 
v2
Comments
Maciej Los 14-May-13 17:07pm    
Nice, you did it yourself!
The problems is solved using grouping. It could look like this: "start_pattern(.)end_pattern" ("end_pattern" is "TTL" in your case). The brackets denote a group. Now, in code, you find the Regex group corresponding the dot in the middle.

Please see: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.match.groups.aspx[^].

Got the idea?

—SA
 
Share this answer
 
v2
try this

C#
string xmlval = "<@>asdfsadfsadf</@>";
                string descriptval = xmlval.Substring(xmlval.LastIndexOf("<@>"));
                string findfrom = "<@>";
                string findto = "</@>";
                int start = descriptval.IndexOf(findfrom);
                int to = descriptval.IndexOf(findto, start + findfrom.Length);
                if (start < 0 || to < 0)
                    return;
                else
                    msg = descriptval.Substring(start + findfrom.Length, to - start - findfrom.Length);

                Console.WriteLine(msg);
                Console.ReadLine();
 
Share this answer
 
just made some changes in mt regex and it works
C#
(?:\b)([0-9]{0,}[.][0-9]{2})(?=TTL)
 
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