Hi,
I want to filter text lines with regex but I couldnt be able to solve. Text is like below;
*************************
this is sample
123.123.123.123 pe 1,
34.34.34.34,
3.3.3.3 te,
another text
anether text too
45.45.45.45
*************************
I want to get the lines which has started with 2 space and continuing decimal character. How may I do this?
Loading

VulpesRank #55 · 29.2K reputationPosted Jul 25, 2012, 5:25 AM EDT
I've changed the test file to:
The output was:
VulpesRank #55 · 29.2K reputationPosted Jul 25, 2012, 12:12 AM EDT
Jignesh TrivediRank #16 · 50.3K reputationPosted Jul 25, 2012, 12:14 AM EDT
your requirement is not much clear but
try...
string[] lines = System.IO.File.ReadAllLines(@"E:\JTtest.txt");
var targetline = lines.Where(file => file.StartsWith(" ") && Regex.IsMatch(file.Trim(), @"^(\d+\.\d+\.\d+\.\d+)(,)$"));
foreach (var mys in targetline)
{
Console.Write(mys);
}
I have create code that filter record which are start with 2 space and match pattern "ddd.ddd.ddd.ddd"
hope this will work for you.
let me know if you required more help.
yokzuRank #5,421 · 300 reputationPosted Jul 25, 2012, 4:43 AM EDT
I'll try to explain it.
In the text file there are lots of lines. Some of them ending with comma, some of them not. For example I want to filter these lines in the sample text above.
*********************
123.123.123.123 pe 1
34.34.34.34
3.3.3.3 te
45.45.45.45
********************
Rules are ;
-Line should start with space and a decimal string
-Should filter till end of line,
-And if line has comma at the end, it should be filtered too.
I hope I make myself clear.
VulpesRank #55 · 29.2K reputationPosted Jul 25, 2012, 4:54 AM EDT
1. Should the line begin with one space (you said two originally) or with either one or two spaces?
2. Will there always be four sets of digits separated by periods, will these sets always contain the same number of digits and will there only be one, two or three digits in each set?
3. What are the rules about 'pe' and 'te' and any following digits?
Jignesh TrivediRank #16 · 50.3K reputationPosted Jul 25, 2012, 5:12 AM EDT
Try...
string[] lines = System.IO.File.ReadAllLines(@"E:\JTtest.txt");
var targetline = lines.Where(file => file.StartsWith(" ") && Regex.IsMatch(file.Trim(), @"[0-9.]+$"));
example
*************************
this is sample
123.123.123.123 pe 1,
34.34.34.34,
3.3.3.3 te,
another text
anether text too
45.45.45.45
*************************
it will only filter " 45.45.45.45"
hope this will help you.
yokzuRank #5,421 · 300 reputationPosted Jul 25, 2012, 5:12 AM EDT
-Yes, the line should start with 2 spaces,
-Line has Ip address and subnet informations like below,
****************
21.43.24.0/24 pe 24,
33.10.24.0/24,
****************
-There are only two strings "pe" and "te". and after them one space and two or one digit decimal text. Like below (And maybe comma..Some of them hasnt.)
*****************
21.43.24.0/24 pe 24,
*****************
yokzuRank #5,421 · 300 reputationPosted Jul 25, 2012, 10:13 PM EDT
Is there any way to filter commas which end of the lines? Some of them has comma but some of them hasnt.
VulpesRank #55 · 29.2K reputationPosted Jul 26, 2012, 1:02 AM EDT
I tried to filter it out using the regular expression itself by replacing ,?$ with a positive lookahead (?=,?$) which in theory should match strings ending with a comma but not include the comma in the result. However, for some unknown reason it didn't work - the same lines were returned but with the comma still in tact :(
yokzuRank #5,421 · 300 reputationPosted Jul 26, 2012, 2:49 AM EDT
18.56.11.44/20 pe 24,
18.56.11.3/20 pe 24,
All decimal digits are between 0-255. Actually they are Ip addresses.
VulpesRank #55 · 29.2K reputationPosted Jul 26, 2012, 3:13 AM EDT
If you change it to this, then it should filter any suffix:
string regExp = @"^ (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(/\d{1,3})?)( (pe|te)( \d{1,2})?)?,?$";
yokzuRank #5,421 · 300 reputationPosted Jul 26, 2012, 5:05 AM EDT