正则表达式-仅读取前4行 [英] Regular expression - reading only first 4 lines
问题描述
伙计们,
我有一个关于正则表达式的问题.
我必须从``EFF:''之后的文本中获取数据(8个字符). 1-4行之间的任何位置,但只有第一个匹配项.文本可以超过10行.让我知道如何限制正则表达式最多只能读取4行. (在这里输入是行的结尾)在此先感谢
文字:
H 01/05/2006 16:34:11 MUNI/CORP 2 16706 1 .................................... ENT:01/05/06:(2006)
DEAL#:1 :TEXT#:1928906:备注:CUSIP#CHG:IND:= N M: EFF :05/01/09:(2009)
TARGET :6497168M2::纽约C:INIT:64971K6F8::9999999999:EXP:05/01/09:(2009)
CASH:1010.00000:::A:STATE:NY:T.AGENT:M012:INFO: EFF ::MAT:05/01/29:(2029)
RATE:5.00000::::1.00000:M:DTC::PRATE:0.000000: PRO:12/28/05:(2005)
DESC. 1:纽约市过渡性金融管理局
DESC. 2:未来税收第二笔收入债券
DESC. 3:系列1999 C MBIA-IBC
EFF :asdwetrt div>
您可以尝试以下方法:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //Your example string string text = @"H 01/05/2006 16:34:11 MUNI/CORP 2 16706 1 ........................................................... ENT:01/05/06:(2006) DEAL#: 1: TEXT#:1928906: REMARKS:CUSIP # CHG : IND:=N M: EFF:05/01/09:(2009) TARGET:6497168M2: :NEW YORK C: INIT:64971K6F8: :9999999999: EXP:05/01/09:(2009) CASH:1010.00000: :A: STATE:NY: T.AGENT:M012: INFO: EFF: : MAT:05/01/29:(2029) RATE: 5.00000: : : : 1.00000:M:DTC: : PRATE: 0.000000: PRO:12/28/05:(2005) DESC. 1: NEW YORK CITY TRANSITIONAL FINANCE AUTHORITY DESC. 2: FUTURE TAX SECOND REVENUE BONDS DESC. 3: SERIES 1999 C MBIA-IBC EFF:asdwetrt "; //Split your string up by using the carriage return escape charater. string[] textSplit= text.Split('\r'); //The regex to find your data Regex expression = new Regex("[E][F][F][:]"); //A list to hold the data you find List<string> foundData = new List<string>(); //This sets the loop to look in only the frist 4 lines of text //We can do this because we split it by the carriage return for (int x = 0; x < 4;x++ ) { //Looks for "EFF: in only one line of the text" Match found = expression.Match(textSplit[x]); //If it finds something if (found.Success) { //We need to add 4 to the index it was found at because it gives //the index of the first charater of "EFF:" int foundindex = found.Index + 4; //This then gets the next 8 chars after the "EFF:" string whatyourlookingfor = textSplit[x].Substring(foundindex, 8); //Add it to the list foundData.Add(whatyourlookingfor); //This displays the data. Console.WriteLine(whatyourlookingfor); } } } } }
Hi Guys,
I have one problem regarding the regular expression.
I have to fetch the data (8 chars) from the text which starts after "EFF:" anywhere between 1-4 lines but only the first match. the text can have more than 10 lines. let me know how to restrict the regex for reading upto only 4 lines. (here enter is the end of line) Thanks in advance
Text:
H 01/05/2006 16:34:11 MUNI/CORP 2 16706 1
........................................................... ENT:01/05/06:(2006)
DEAL#: 1: TEXT#:1928906: REMARKS:CUSIP # CHG : IND:=N M: EFF:05/01/09:(2009)
TARGET:6497168M2: :NEW YORK C: INIT:64971K6F8: :9999999999: EXP:05/01/09:(2009)
CASH:1010.00000: :A: STATE:NY: T.AGENT:M012: INFO: EFF: : MAT:05/01/29:(2029)
RATE: 5.00000: : : : 1.00000:M:DTC: : PRATE: 0.000000: PRO:12/28/05:(2005)
DESC. 1: NEW YORK CITY TRANSITIONAL FINANCE AUTHORITY
DESC. 2: FUTURE TAX SECOND REVENUE BONDS
DESC. 3: SERIES 1999 C MBIA-IBC
EFF:asdwetrt
You could try this:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //Your example string string text = @"H 01/05/2006 16:34:11 MUNI/CORP 2 16706 1 ........................................................... ENT:01/05/06:(2006) DEAL#: 1: TEXT#:1928906: REMARKS:CUSIP # CHG : IND:=N M: EFF:05/01/09:(2009) TARGET:6497168M2: :NEW YORK C: INIT:64971K6F8: :9999999999: EXP:05/01/09:(2009) CASH:1010.00000: :A: STATE:NY: T.AGENT:M012: INFO: EFF: : MAT:05/01/29:(2029) RATE: 5.00000: : : : 1.00000:M:DTC: : PRATE: 0.000000: PRO:12/28/05:(2005) DESC. 1: NEW YORK CITY TRANSITIONAL FINANCE AUTHORITY DESC. 2: FUTURE TAX SECOND REVENUE BONDS DESC. 3: SERIES 1999 C MBIA-IBC EFF:asdwetrt "; //Split your string up by using the carriage return escape charater. string[] textSplit= text.Split('\r'); //The regex to find your data Regex expression = new Regex("[E][F][F][:]"); //A list to hold the data you find List<string> foundData = new List<string>(); //This sets the loop to look in only the frist 4 lines of text //We can do this because we split it by the carriage return for (int x = 0; x < 4;x++ ) { //Looks for "EFF: in only one line of the text" Match found = expression.Match(textSplit[x]); //If it finds something if (found.Success) { //We need to add 4 to the index it was found at because it gives //the index of the first charater of "EFF:" int foundindex = found.Index + 4; //This then gets the next 8 chars after the "EFF:" string whatyourlookingfor = textSplit[x].Substring(foundindex, 8); //Add it to the list foundData.Add(whatyourlookingfor); //This displays the data. Console.WriteLine(whatyourlookingfor); } } } } }
这篇关于正则表达式-仅读取前4行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!