Suppose that the maximum IP can contain the maximum number of 999 in each "dot" bracket,i.e. 999.999.999.999 is the largest available. I've checked the regex ([0-9]+.){3}[0-9] in the calculator. So, why the program throws the run-time error "parsing "?([0-9]+.){3}[0-9]" - Quantifier {x,y} following nothing."?
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace RegExCs
{
class Program
{
static void Main(string[] args)
{
string rawData;
Regex filter = new Regex(#"?<ip>([0-9]+\.){3}[0-9]"); //run-time error
rawData=File.ReadAllText("Query list");
MatchCollection theMatches = filter.Matches(rawData);
foreach (Match theMatch in theMatches)
{
Console.WriteLine("ip: {0}\n",theMatch.Groups["ip"]);
}
Console.ReadKey();
}
}
}
Official help page wasn't so helpful for me.
"Query list" file content:
Reply from 212.77.100.101 www.wp.pl time: 21:37
Reply from 111.41.130.55 www.dupa.pl time: 05:33
Reply from 230.77.100.101 www.whatanannoyingbug.com time: 04:12
Reply from 65.77.100.101 www.foooo.org time: 12:55
Reply from 200.77.100.101 www.example.com time: 07:56
You need to surround your entire regex with parentheses, change ?<ip>([0-9]+\.){3}[0-9] to the following:
(?<ip>([0-9]+\.){3}[0-9])
This is necessary because the ?<name> syntax for creating a named group only works immediately following an opening parentheses, otherwise ? means "make the previous element optional". Since there is no previous element before the ?, you are getting an error.
I would use this to ensure it's a REAL (0-255) IP address:
(([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))\.){3}([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))
Related
The program is supposed to look for a string in a line, and if it finds the string, it will make the inserts after meeting the condition inside the textfile. Currently, when I run this program it is now simply giving me a blank console. Previously, I had it just reading all the lines properly and could make inserts only if I remove them first but it messed the indexing up and ultimately did not give me the result I wanted. The logic is fairly straightforward, if you see any problems please share your thoughts. Please and thanks. I am very confused why this is having problems and not working.
using System.IO;
using System.Globalization;
using System.Diagnostics;
using System.Text;
using System.Linq;
using System.Linq.Expressions;
namespace Masker
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine();
string path = #"\file1.txt";
ReadLines(path)
}
public static void ReadLines(string path)
{
int counter = 0;
var text = new StringBuilder();
foreach (string s in File.ReadAllLines(path))
{
counter += 1;
if (s.Contains("000INDEX"))
{
text.AppendLine(s.Insert(60, "#"));
}
else if (s.Contains("001PRTBNR"))
{
text.AppendLine(s.Insert(60, "#").Insert(119,"#").Insert(120,"#").Insert(121, "#"));
};
text.AppendLine(s);
//Console.Write(text.ToString());
}
Console.Write(text.ToString());
}
}
}
The last two blocks of your if/else statement will never be executed.
If the execution reaches the third check
else if (s.Contains("000INDEX"))
that will always be true. Because if it wasn't, then the first check
if (!s.Contains("000INDEX"))
would have already been true.
But the biggest problem is that if the line contains "000INDEX", your while loop becomes and infinite loop. You never leave it. That is probably the reason why you end up with a blank console.
I'm using windows 10 (maybe this is the problem:-) )
I have a simple code that reads text in Hebrew from console them print it's HEX\DEC value
but he give me 00 all the time
on the console window I can see the Hebrew letters
any reason why?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Net;
using System.Net.Sockets;
using System.Timers;
using System.IO;
namespace HebTest
{
class Program
{
static public void Main(string[] args)
{
Console.WriteLine("Write your message here - ");
string StringMessage = Console.ReadLine();
Console.WriteLine("print string - " + StringMessage);
///message in HEX
byte [] ByteMessage = Encoding.Default.GetBytes(StringMessage);
string HexMessage = BitConverter.ToString(ByteMessage);
Console.WriteLine("MSG in HEX - " + HexMessage);
Console.Write(
Encoding.Default.GetString(ByteMessage)
);
Console.WriteLine();
foreach (byte p in ByteMessage)
{
Console.Write((char)p + " - " + p );
Console.WriteLine("");
}
}
for example I enter this text
"test אבגד"
and this is what I got :
Write your message here -
test אבגד ---> this I wrote on the console
print string - test
MSG in HEX - 74-65-73-74-20-00-00-00-00
test
t - 116
e - 101
s - 115
t - 116
- 32
- 0
- 0
- 0
- 0
what am I missing?
Thanks ,
You're using Encoding.Default to convert the string into binary. That's almost always a bad idea - it means the same code may work on some machines and not on others. It's pretty much only useful when you want to read/write a text file on that machine, and you're sure that the system default encoding is the right one to use for any machine that runs it. That's rare.
In particular, you're trying to talk to an external device - which means you need to use the encoding it expects. You should find that out, and use the appropriate encoding. For example, if the device expects UTF-8, use:
// Local variable names changed to be more idiomatic C#
string text = Console.ReadLine();
byte[] bytes = Encoding.UTF8.GetBytes(text);
As you've now indicated that the device expects Windows-1255, that's code page 1255, so you get the encoding like this:
Encoding.GetEncoding(1255)
For clarify, I'd probably use a separate variable for that:
string text = Console.ReadLine();
Encoding encoding = Encoding.GetEncoding(1255);
byte[] bytes = encoding.GetBytes(text);
Found the problem (after #Daisy Shipton let me think )
the code is OK
need to go to
control Panel - region - administrative - system locale -
REMOVE V on Beta : use Unicode UTF-8 for worldwide language support
hope it will help someone someday
An unhandled exception of type OpenQA.Selenium.InvalidSelectorException occurred in WebDriver.dll
Additional information: invalid selector: Unable to locate an element with the xpath expression //*[#id='hdtb-msb']/div[1]/div[4]/a) because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[#id='hdtb-msb']/div[1]/div[4]/a)' is not a valid XPath expression.
I have tried finding the element through Class as well -- didn't seem to be working. Then remembered that it could be a time issue. After reading several articles/posts, I am not sure what else to try.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace Webdemo2
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver();
driver.Url = "http://gooogle.com";
var searchbox = driver.FindElement(By.Id("lst-ib"));
searchbox.SendKeys("Pluralsight");
//adding timeout
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(7));
var images = driver.FindElement(By.XPath("//*[#id='hdtb-msb']/div[1]/div[4]/a)"));
images.Click();
}
}
}
At the end of your xpath expression:
/a)
I think you need to take that closing parenthesis out of the expression.
I've just installed Solr/Lucene on a Windows machine simply to test its capability. I've indexed a couple hundred files and I'm able to successfully query the indexed content through the web interface. Given that the web interface isn't too user friendly I thought I'd try to create a simple application to do a full text search over the indexed content. So far I have:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Lucene.Net;
namespace solrTest
{
class Program
{
static void Main(string[] args)
{
string indexFileLocation = #"C:\solr-5.3.1\server\solr\test\data\index";
Lucene.Net.Store.Directory dir = Lucene.Net.Store.FSDirectory.Open(indexFileLocation);
Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(dir);
Lucene.Net.Index.Term searchTerm = new Lucene.Net.Index.Term("ID", "1");
Lucene.Net.Search.Query query = new Lucene.Net.Search.TermQuery(searchTerm);
Lucene.Net.Search.TopDocs results = searcher.Search(query, null, 10);
foreach(Lucene.Net.Search.ScoreDoc test in results.ScoreDocs)
{
Console.WriteLine(test.ToString());
Console.ReadLine();
}
}
}
}
When running the code I receive an error stating:
An unhandled exception of type 'System.IO.IOException' occurred in Lucene.Net.dll
Additional information: read past EOF
I know I'm missing something obvious. Your help would be greatly appreciated.
Edit: I downloaded Luke.Net and received the same error.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
namespace SchoolPasswordLockFolder
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Enter the password: "); // the space character after the semicolon has an error
public string input = Console.ReadLine();
}
}
}
The errors:
Severity Code Description Project File Line
Error CS1513 } expected SchoolPasswordLockFolder c:\Users\CENSOREDSIJGIOFSGJIOFS\documents\visual studio 2015\Projects\App5\SchoolPasswordLockFolder\SchoolPasswordLockFolder\Program.cs 14
(for the one after the semicolon)
and
Severity Code Description Project File Line
Error CS1022 Type or namespace definition, or end-of-file expected SchoolPasswordLockFolder c:\Users\CENSOREDIDONTWANTSTALKERS\documents\visual studio 2015\Projects\App5\SchoolPasswordLockFolder\SchoolPasswordLockFolder\Program.cs 19
(for the last bracket)
I have not programmed in C# for a very long time as I was too busy with web development and lua...
change this:
public string input = Console.ReadLine();
to:
string input = Console.ReadLine();
local variables do not get accessibility modifiers like public.