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
VulpesPosted Jul 25, 2012, 9:25 AM
I've changed the test file to:
The output was:
yokzuPosted Jul 26, 2012, 9:05 AM
VulpesPosted Jul 26, 2012, 7:13 AM
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})?)?,?$";
yokzuPosted Jul 26, 2012, 6:49 AM
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.
VulpesPosted Jul 26, 2012, 5:02 AM
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 :(
yokzuPosted Jul 26, 2012, 2:13 AM
Is there any way to filter commas which end of the lines? Some of them has comma but some of them hasnt.
yokzuPosted Jul 25, 2012, 9:12 AM
-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,
*****************
Jignesh TrivediPosted Jul 25, 2012, 9:12 AM
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.
VulpesPosted Jul 25, 2012, 8:54 AM
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?
yokzuPosted Jul 25, 2012, 8:43 AM
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.
Jignesh TrivediPosted Jul 25, 2012, 4:14 AM
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.
VulpesPosted Jul 25, 2012, 4:12 AM