如何在两个符号之间提取字符串 [英] how to extract string between two symbols
本文介绍了如何在两个符号之间提取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,我想在TTL之前使用正则表达式获取数字我怎么能这样做
这是我的字符串
WPNCB«
19AUG离开日期-----最后一天购买21MAY / 2359
基本票价EQUIV AMT税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 // 此处是TTL 跨度>
ADT-01 EPX NPX
CPH SU X / MOW SU HRK78.68EPX SU X / MOW SU CPH59.45NPX NUC138。<跨度类= 代码位数> 13
END ROE5。 71905
有效的SU / FARE RESTR APL
确认承运人 - SU
< pre lang = c# >
i使用\d { 1 , 4 } \\ \\d { 2 } TTL正则表达式,但它获取TTL,我不想显示TTL线。
在此先感谢
解决方案
我刚刚在我的正则表达式中进行了一些更改并且它正常工作/>
(?:\ b)([0-9] { 0 ,} [。] [0-9] { 2 })(?= TTL)
< blockquote>使用分组解决问题。它可能如下所示:start_pattern(。)end_pattern(在您的情况下,end_pattern为TTL)。括号表示一个组。现在,在代码中,您会发现Regex组对应于中间的点。
请参阅:http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.match.groups.aspx [ ^ ]。
有这个主意吗?
试试这个
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 )
返回跨度>;
else
msg = descriptval.Substring(start + findfrom.Length,to - start - findfrom.Length);
Console.WriteLine(msg);
Console.ReadLine();
I have a string in which i want to fetch number before TTL using regex how can i do that
this is my string
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
解决方案
i just made some changes in my regex and it works
(?:\b)([0-9]{0,}[.][0-9]{2})(?=TTL)
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
try this
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();
这篇关于如何在两个符号之间提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文