-3

大家好,我也是新的正则表达式世界我认为我有一个我必须工作的代码,所以我只能修改那个代码我正在做的是我必须从字符串列表中找到一个票号,但我得到了这个错误长度不能小于零我添加一个检查当我添加一个检查时我得到一个错误,startindex不能小于零我正在编写我的代码详细信息

我的 aspx.cs 页面:

using System.Windows.Forms;
using System.Data;
namespace AQCConsolidator1
{
public partial class Form1 : Form
{
    private void btnGetData_Click(object sender, EventArgs e)
    {
        List<String> list = new List<String>();
        List<String> list1 = new List<String>();

        string History = @"RP/DCA1S219C/DCA1S219C            SL/SU   7FEB13/2215Z   8TXSAK                 
 36 FA PAX 125-7239038457/ETBA/07FEB13/DCA1S211E/49881134                       
   /S4-5/P3                                                                 
 37 FA PAX 125-7239038458/ETBA/07FEB13/DCA1S211E/49881134                       
   /S4-5/P1                                                                 
 38 FA PAX 125-7239038459/ETBA/07FEB13/DCA1S211E/49881134                       
   /S4-5/P2                                                                 
  023 AF/FA 125-7239038457/ETBA/07FEB13/DCA1S211E/49881134/BA                 
    272 N 24JUN SANLHR/BA 273 N 07AUG LHRSAN/MACDORMAN/SAM                  
    CORBIN(CHD)                                                             
  023 AF/FA 125-7239038458/ETBA/07FEB13/DCA1S211E/49881134/BA                 
    272 N 24JUN SANLHR/BA 273 N 07AUG LHRSAN/LOJ/DANUTA                     
    ISABELLA                                                                
  023 AF/FA 125-7239038459/ETBA/07FEB13/DCA1S211E/49881134/BA                 
    272 N 24JUN SANLHR/BA 273 N 07AUG LHRSAN/MACDORMAN/JACK                 
    HENRY                                                                   
  023 RF-RH CR-DCA1S211E 49881134 GS 1003RH/RO-67ECDAB1 DCA1S2                
    10W 49592222 07FEB0628Z ";

        string Histroy1 = @"RP/DCA1S219C/DCA1S219C            SL/SU   5APR13/2119Z   23ZEBK                 
   42 FA PAX 220-7242648582-83/ETLH/USD1166.75/04APR13/DCA1S211E/4                
   9881134/S3-7/P1                                                          
   43 FA PAX 220-7242648584-85/ETLH/USD1166.75/04APR13/DCA1S211E/4                
   9881134/S3-7/P2                                                          
   022 AF/FA 220-7242636221-22/ETLH/USD1166.75/03APR13/                        
    NYC1S21DD/33895934/OU 341 S 13MAY SJJZAG/LH6001 T 13MAY                 
    ZAGFRA/LH 402 T 13MAY FRAEWR/LH7608 T 17JUL EWRMUC/                     
    LH1730 T 18JUL MUCSJJ/TRESNJO/SABAHETA(ADT)                             
   022 AF/FA 220-7242636223-24/ETLH/USD1166.75/03APR13/                        
    NYC1S21DD/33895934/OU 341 S 13MAY SJJZAG/LH6001 T 13MAY                 
    ZAGFRA/LH 402 T 13MAY FRAEWR/LH7608 T 17JUL EWRMUC/                     
    LH1730 T 18JUL MUCSJJ/TRESNJO/FEJZO(ADT)                                
   022 RF-WS-NMC-US/WSTNGTNG CR-NYC1S21DD 33895934 SU 9999WS/RO                
    -9C9482E6 NYCW33TNG 00000000 04APR0223Z                                 
 022/033 XF/FA 220-7242636221-22/ETLH/USD1166.75/03APR13/                        
    NYC1S21DD/33895934/OU 341 S 13MAY SJJZAG/LH6001 T 13MAY                 
    ZAGFRA/LH 402 T 13MAY FRAEWR/LH7608 T 17JUL EWRMUC/                     
    LH1730 T 18JUL MUCSJJ/TRESNJO/SABAHETA(ADT)                             
   033 AF/FA 220-7242636221-22/EVLH/USD1166.75/03APR13/                        
    NYC1S21DD/33895934/OU 341 S 13MAY SJJZAG/LH6001 T 13MAY                 
    ZAGFRA/LH 402 T 13MAY FRAEWR/LH7608 T 17JUL EWRMUC/                     
)>md
 RP/DCA1S219C/DCA1S219C            SL/SU   5APR13/2119Z   23ZEBK                 
    LH1730 T 18JUL MUCSJJ/TRESNJO/SABAHETA(ADT)                             
033 RF-2G-NYC1S21SP/00102G CR-NYC1S21DD 33895934 GS 00102G/R                
    O-67ED3FEE NYC1S21SP 33608293 04APR0514Z                                
022/034 XF/FA 220-7242636223-24/ETLH/USD1166.75/03APR13/                        
    NYC1S21DD/33895934/OU 341 S 13MAY SJJZAG/LH6001 T 13MAY                 
    ZAGFRA/LH 402 T 13MAY FRAEWR/LH7608 T 17JUL EWRMUC/                     
    LH1730 T 18JUL MUCSJJ/TRESNJO/FEJZO(ADT)                                
   034 AF/FA 220-7242636223-24/EVLH/USD1166.75/03APR13/                        
    NYC1S21DD/33895934/OU 341 S 13MAY SJJZAG/LH6001 T 13MAY                 
    ZAGFRA/LH 402 T 13MAY FRAEWR/LH7608 T 17JUL EWRMUC/                     
    LH1730 T 18JUL MUCSJJ/TRESNJO/FEJZO(ADT)                                
   034 RF-2G-NYC1S21SP/00102G CR-NYC1S21DD 33895934 GS 00102G/R                
    O-67ED3FEE NYC1S21SP 33608293 04APR0514Z                                
 033/035 XF/FA 220-7242636221-22/EVLH/USD1166.75/03APR13/                        
    NYC1S21DD/33895934/OU 341 S 13MAY SJJZAG/LH6001 T 13MAY                 
    ZAGFRA/LH 402 T 13MAY FRAEWR/LH7608 T 17JUL EWRMUC/                     
    LH1730 T 18JUL MUCSJJ/TRESNJO/SABAHETA(ADT)                             
 034/035 XF/FA 220-7242636223-24/EVLH/USD1166.75/03APR13/                        
    NYC1S21DD/33895934/OU 341 S 13MAY SJJZAG/LH6001 T 13MAY                 
    ZAGFRA/LH 402 T 13MAY FRAEWR/LH7608 T 17JUL EWRMUC/                     
    LH1730 T 18JUL MUCSJJ/TRESNJO/FEJZO(ADT)                                
        string History2 = @"RP/DCA1S219C/DCA1S219C            SL/SU   9APR13/2223Z   4YYTR7                 
 45 FA PAX 257-7242858184/ETOS/USD736.23/08APR13/DCA1S211E/49881                
   134/S3-6/P1                                                              
 46 FA PAX 257-7242858185/ETOS/USD736.23/08APR13/DCA1S211E/49881                
   134/S3-6/P2                                                              
  030 AF/FA 257-7242215364/ETOS/USD778.23/07APR13/DCA1S219C/                  
    49881134/OS 850 K 31MAY TIAVIE/OS 087 K 31MAY VIEJFK/OS                 
    088 L 09JUN JFKVIE/OS 847 L 10JUN VIETIA/MBYETI/PETRAQ(                 
    ADT)                                                                    
  030 AF/FA 257-7242215365/ETOS/USD778.23/07APR13/DCA1S219C/                  
    49881134/OS 850 K 31MAY TIAVIE/OS 087 K 31MAY VIEJFK/OS                 
    088 L 09JUN JFKVIE/OS 847 L 10JUN VIETIA/MBYETI/JORGJIE(                
    ADT)                                                                    
   030 RF-WS-NMC-US/WSTMTFTP CR-DCA1S219C 49881134 SU 9999WS/RO                
    -9CCF46CE TNGW33TMT 00000000 07APR1829Z                                 
  030/035 CF/FA 257-7242215364/ETOS/USD778.23/07APR13/DCA1S219C/                  
    49881134/OS 850 K 31MAY TIAVIE/OS 088 L 09JUN JFKVIE/OS                 
    087 K 31MAY VIEJFK/OS 847 L 10JUN VIETIA/MBYETI/PETRAQ(                 
    ADT)                                                                    
 030/035 AF/FA 257-7242215364/ETOS/USD778.23/07APR13/DCA1S219C/                  
    49881134/OS 850 K 31MAY TIAVIE/OS 087 K 31MAY VIEJFK/                   
    MBYETI/PETRAQ(ADT)                                                      
 )>md
  RP/DCA1S219C/DCA1S219C            SL/SU   9APR13/2223Z   4YYTR7                 
  030/035 CF/FA 257-7242215365/ETOS/USD778.23/07APR13/DCA1S219C/                  
    49881134/OS 850 K 31MAY TIAVIE/OS 088 L 09JUN JFKVIE/OS                 
    087 K 31MAY VIEJFK/OS 847 L 10JUN VIETIA/MBYETI/JORGJIE(                
    ADT)                                                                    
  030/035 AF/FA 257-7242215365/ETOS/USD778.23/07APR13/DCA1S219C/                  
    49881134/OS 850 K 31MAY TIAVIE/OS 087 K 31MAY VIEJFK/                   
    MBYETI/JORGJIE(ADT)                                                     
   035 RF-P-NYC1S21SP/44744U CR-NYC1S21DD 33895934 GS 44744U/RO                
    -67EC76D0 NYC1S21SP 33608293 08APR1143Z                                 
  035/043 XF/FA 257-7242215364/ETOS/USD778.23/07APR13/DCA1S219C/                  
    49881134/OS 850 K 31MAY TIAVIE/OS 087 K 31MAY VIEJFK/                   
    MBYETI/PETRAQ(ADT)                                                      
  043 AF/FA 257-7242215364/EVOS/USD778.23/07APR13/DCA1S219C/                  
    49881134/OS 850 K 31MAY TIAVIE/OS 087 K 31MAY VIEJFK/                   
    MBYETI/PETRAQ(ADT)                                                      
   043 RF-SG CR-DCA1S211E 49881134 GS 0810SG/RO-67ECDAB1 DCA1S2                
    10W 49592222 08APR1217Z                                                 
 035/045 XF/FA 257-7242215365/ETOS/USD778.23/07APR13/DCA1S219C/                  
    49881134/OS 850 K 31MAY TIAVIE/OS 087 K 31MAY VIEJFK/                   
    MBYETI/JORGJIE(ADT)                                                     
  045 AF/FA 257-7242215365/EVOS/USD778.23/07APR13/DCA1S219C/                             
        list.Add(History);
        list.Add(Histroy1);
        list.Add(History2);

        list1.Add(RTQ);
        list1.Add(RTQ1);

        MyClass1 mc = new MyClass1();
        DataTable dt = mc.GetNumber(list);
    }
}

}

班级代码::

 using System.Data;
 using System.Text.RegularExpressions;

namespace AQCConsolidator1
{
class MyClass1
{
    string output;
    string subs;

    DataTable dt = new DataTable();
    DataRow dr = null;
    public DataTable GetNumber(List<string> request)
    {
        dt.Columns.Add("TickitNumber", typeof(string));

        foreach (string data in request)
        {
            string item = data;
            while (item.Trim() != "")
            {
                dr = dt.NewRow();
                int r1 = item.IndexOf("\r", 3);
                if(r1>0)
                {
                subs = item.Substring(0, r1);
                }
                if (subs.Contains("ET"))
                {
                    Regex regexs = new Regex(@"[0-9||-]{14}");
                    foreach (Match m in regexs.Matches(subs))
                    {
                        output = m.ToString();
                        dr["TickitNumber"] = output;
                    }
                    dt.Rows.Add(dr);
                }
                    item = item.Substring(subs.Length);

                    r1 = item.IndexOf("\r\n", 0);

                    item = item.Substring(r1);///Error in this line StartIndex cannot be less than zero. 
            }
        }
        return dt;
    } 
}

}

4

1 回答 1

0

尝试使用

if(!string.IsNullOrEmpty(item))
{
  r1 = item.IndexOf(Environment.NewLine, 0);
  if(r1 >0)
    item = item.Substring(r1);
}
于 2013-05-02T11:13:24.740 回答