C# to find out all the lines contain "33"

spyder

Client
Регистрация
19.10.2014
Сообщения
27
Благодарностей
1
Баллы
3
hi, guys,
i have a list as below:
11a
22b
33c
44d
55e
33f

how to use C# to find out all the lines contain "33", and put the line numbers to List2 ?

[i know that i can take one line, then use REG to check it contains value or not.
BUT, cause i have many lines. it will take too long time to check line one by one.]

anyone knows how to use C# to do it ?

thank you very much.
 

globalledstore

Пользователь
Регистрация
13.12.2013
Сообщения
40
Благодарностей
4
Баллы
8
hi, lokiys
thanks for the treads. it do help me a lot. but it save the line value, and i want to save the lines count.
according to the threads, it will save :
33c
33f
but i want to save the
2 //"33c" is on the 2nd line
5 //"33f" is on the fifth line

how to edit it the code, please ?

Код:
// prepare regular expression for parsing
var parserRegexPattern = project.Variables["listSearchRegex"].Value;
var parserRegex = new System.Text.RegularExpressions.Regex(parserRegexPattern);
// get a list for searching
var sourceList = project.Lists["SourceList"];
// get result list
var destList = project.Lists["OutputList"];
// search in each line of the list
lock(SyncObjects.ListSyncer)
{
    for(int i=0; i < sourceList.Count; i++)
    {
        // take one line from the list
        var str = sourceList[i];
        // check the line on matches by regex, if there are matches we put it to result list
        if (parserRegex.IsMatch(str))
        {
            destList.Add(str);       
        }
    }
}
 

Radzhab

Client
Регистрация
23.05.2014
Сообщения
1 500
Благодарностей
1 268
Баллы
113
destList.Add(i+1);
 

globalledstore

Пользователь
Регистрация
13.12.2013
Сообщения
40
Благодарностей
4
Баллы
8
i got it. hahah,
Код:
// prepare regular expression for parsing
var parserRegexPattern = project.Variables["listSearchRegex"].Value;
var parserRegex = new System.Text.RegularExpressions.Regex(parserRegexPattern);
// get a list for searching
var sourceList = project.Lists["SourceList"];
// get result list
var destList = project.Lists["OutputList"];
// search in each line of the list
lock(SyncObjects.ListSyncer)
{
    for(int i=0; i < sourceList.Count; i++)
    {
        // take one line from the list
        var str = sourceList[i];
        // check the line on matches by regex, if there are matches we put it to result list
        if (parserRegex.IsMatch(str))
        {
            destList.Add("" +i );     
        }
    }
}
 
  • Спасибо
Реакции: Radzhab

Radzhab

Client
Регистрация
23.05.2014
Сообщения
1 500
Благодарностей
1 268
Баллы
113
if you add
destList.Add("" +i );
than you got
33c 0
33c 1
numbering starts from zero)
Yo must write
destList.Add("" +i +1);
 

globalledstore

Пользователь
Регистрация
13.12.2013
Сообщения
40
Благодарностей
4
Баллы
8
  • Спасибо
Реакции: Radzhab

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)