我需要获取用户放入文本框的值
输入
交易ID:100000527054518 PNR号:6755980353列车编号/ 姓名:18615 / KRIYA YOGA EXP预订日期:2016年06月07日课程: SLEEPER CLASS配额:一般旅行日期:2016年6月13日来自: HWH To:RNC登机时间:HWH登机日期:2016年6月13日 预订最多:RNC距离:416公里预定出发时间:22:10 预定抵达时间:2016年6月14日(07:05)总票价:? 500.0& SC:? 23.0成人:2&儿童:0乘客的详细信息S.No.Name
年龄性别特许状态教练座位/泊位/ WL无电流 状态教练座位/泊位/ WL无ID类型/ ID号1 AYAN PAL 40公CNF S7 49(LB)CNF S7 49(LB)
string TransactionID="";
string pnrno="";
string trainno="";
string dateofbooking="";
string class="";
string Quota="";
输出
TransactionID=100000527054518 ;
pnrno=6755980353;
trainno=18615;
dateofbooking=13-Jun-2016;
class=SLEEPER CLASS;
Quota=GENERAL;
AND如果Class是AC 3 Tier
TransactionID=100000527054518 ;
pnrno=6755980353;
trainno=18615;
dateofbooking=13-Jun-2016;
class=AC 3 TIER;
Quota=GENERAL;
请帮助我从现在开始坚持这个
答案 0 :(得分:1)
这种方式有效,但它仍然是文本依赖的错误':'将粉碎你的应用程序,
http://ieeexplore.ieee.org/xpls/abs_all.jsp?arnumber=4641337&tag=1
选项1
如果你有权访问源文本,你应该这样写:
"交易ID:100000527054518 | PNR号:6755980353 | ..."
之后您将文字按string source = @"Transaction ID : 100000527054518 PNR No. : 6755980353 Train No. / Name : 18615 / KRIYA YOGA EXP Date of Booking : 07-Jun-2016 Class : SLEEPER CLASS Quota : GENERAL Date of Journey : 13-Jun-2016 From : HWH To : RNC Boarding At : HWH Date Of Boarding : 13-Jun-2016 Reservation Up to : RNC Distance : 416 KM Scheduled Departure : 22:10 Scheduled Arrival : 14-Jun-2016 ( 07:05 Hrs ) Total Fare : ? 500.0 & SC : ? 23.0 Adult : 2 & Child : 0 Details of Passengers S.No.Name
Age Gender Concession Status Coach Seat / Berth / WL No Current Status Coach Seat / Berth / WL No ID Type / ID No. 1 AYAN PAL 40 Male CNF S7 49(LB) CNF S7 49(LB)";
string[] sourceArray = source.Split(':');
string TransactionID = sourceArray[2].Split(' ')[0];
string pnrno = "";
string trainno = "";
string dateofbooking = "";
string classStr="";
string Quota = "";
分割,之后下一次拆分将由(':')完成,因此您将获得split('|')
,{{1} }
:
result[0] = type
如果您无法编辑源代码,则需要使用索引:
result[1] = value
并从索引获取以下值:to:减去下一个类型
例如第一个将是100000527054518 PNR号 - PNR号= 100000527054518
选项2,我认为是最好的
使用常规表达
for(int i = 0 ; i < sourceArray.Count ; i++)
{
string[] resultArr = sourceArray.Split(':');
if(resultArr[0].Equals("Transaction ID")) TransactionId = resultArr[1];
else if ...
}
答案 1 :(得分:0)
您应该使用某种文本解析API。 Split()
在这里不会有太大帮助。您要做的是解析给定的输入并获取令牌数据。有些API就像(但商业化而非免费,尽管他们有30天的试用期)
答案 2 :(得分:0)
好的,我所做的是代表你要求从字符串中获取的变量的类。
该类有一个获取字符串并使用正则表达式解析它的方法。
class Program
{
static void Main(string[] args)
{
string source = @"Transaction ID : 100000527054518 PNR No. : 6755980353 Train No. / Name : 18615 / KRIYA YOGA EXP Date of Booking : 07-Jun-2016 Class : SLEEPER CLASS : GENERAL Date of Journey : 13-Jun-2016 From : HWH To : RNC Boarding At : HWH Date Of Boarding : 13-Jun-2016 Reservation Up to : RNC Distance : 416 KM Scheduled Departure : 22:10 Scheduled Arrival : 14-Jun-2016 ( 07:05 Hrs ) Total Fare : ? 500.0 & SC : ? 23.0 Adult : 2 & Child : 0 Details of Passengers S.No.Name
Age Gender Concession Status Coach Seat / Berth / WL No Current Status Coach Seat / Berth / WL No ID Type / ID No. 1 AYAN PAL 40 Male CNF S7 49(LB) CNF S7 49(LB)";
MyClass mc1 = new MyClass();
mc1.getObjectFromString(source);
}
}
class MyClass
{
public string TransactionID { get; set; }
public string pnrno { get; set; }
public string trainno { get; set; }
public string dateofbooking { get; set; }
public string className { get; set; }
public string Quota { get; set; }
public void getObjectFromString(string source)
{
Regex transactionRegex = new Regex(@"Transaction ID : [0-9]+ PNR No.");
Regex pnrnoRegex = new Regex(@"PNR No. : [0-9]+ Train No. / Name");
Regex trainnoRegex = new Regex(@"Train No. / Name : [0-9]*[A-Za-z/ ]* Date of Booking");
Regex dateofbookingRegex = new Regex(@"Date of Booking : [-0-9a-zA-Z/ ]* Class");
Regex classNameRegex = new Regex(@"Class : [A-Za-z ]* CLASS");
Regex QuotaRegex = new Regex(@"CLASS : [A-Za-z ]* Date of Journey");
Match match = transactionRegex.Match(source);
if (match.Success)
this.TransactionID = match.Value.Replace("Transaction ID :", "").Replace("PNR No.", "");
match = pnrnoRegex.Match(source);
if (match.Success)
this.pnrno = match.Value.Replace("PNR No. :", "").Replace("Train No. / Name", "");
match = trainnoRegex.Match(source);
if (match.Success)
this.trainno = new String(match.Value.Replace("Train No. / Name :", "").Replace("Date of Booking", "").ToCharArray().Where(c => Char.IsDigit(c)).ToArray());
match = dateofbookingRegex.Match(source);
if (match.Success)
this.dateofbooking = match.Value.Replace("Date of Booking :", "").Replace("Class", "");
match = classNameRegex.Match(source);
if (match.Success)
this.className = match.Value.Replace("Class :", "").Replace("CLASS", "");
match = QuotaRegex.Match(source);
if (match.Success)
this.Quota = match.Value.Replace("CLASS :", "").Replace("Date of Journey", "");
}
}