Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to do Regex from a website url, but it gets me an error:
Specified argument was out of the range of valid values
Regex re = new Regex("\"id\":\"([0 - 9] +)\"");
string ree = re.Matches(sr)[0].Value;
MessageBox.Show(ree);
The url output is just blank page with text. http://prntscr.com/a6xyi0
You have to move all the spaces you don't want match, you can use [0-9]+ or \d+
You can iterate over all matches for every id in the string:
Regex re = new Regex("\"id\":\"(?<id>\\d+)\"");
string[] ree = re.Matches(sr).Cast<Match>().Select(m => m.Value).ToArray();
// Or if you just want the id:
string[] ree = re.Matches(sr).Cast<Match>().Select(m => m.Groups["id"].Value).ToArray();
foreach (var item in ree)
{
//do something
}
EDIT:
If you want add the results to a ListView then this should work for you:
var sr = "{\"id\":\"11111\", ...} {\"id\":\"22222\", ...} {\"id\":\"33333\", ...} {\"id\":\"44444\", ...}";
Regex re = new Regex("\"id\":\"(?<id>\\d+)\"");
var ree = re.Matches(sr).Cast<Match>().Select(m => m.Groups["id"].Value);
foreach (var item in ree)
{
var lvItem = new ListViewItem(new string[] { item, "who column" });
listView.Items.Add(lvItem);
}
And you will get some like this:
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I got a task for URL validation in which url should not contain any special characters and it should accept only https. I tried with
^((https)://)?([\w+?\.\w+])+([a-zA-Z0-9\\\/\.\:]*)?$
this regex pattern it works fine for special characters but not working for https, Its accepting http also whereas i need it to accept only https. I googled it but unable to find any solution.
Is regex actually a requirement ?
Actually, it's far more simple (and I guess, efficient) to test for the first 7 characters :
var tests = new String[]{
"http://shouldfail",
"https://shouldsucceed",
"ftp://fail"
};
foreach(var test in tests){
if(test.StartsWith("https://", StringComparison.CurrentCultureIgnoreCase)){
Console.WriteLine(test + " is OK");
}else{
Console.WriteLine(test + " is not OK");
}
}
You could do something like this for example:
static void Main(string[] args)
{
string pattern = #"^(https:\/\/)[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]#!\$&'\(\)\*\+,;=.]+$";
Regex reg = new Regex(pattern);
string[] urls = { "https://test.com", "http://test.com" };
foreach (var item in urls)
{
var test = reg.IsMatch(item);
Console.WriteLine(test.ToString());
}
Console.ReadKey();
}
Result is:
True
False
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I try to search in my list elements with pattern
string pat = #"(a)"; // works
But when I try to use textbox to set pattern it doesent works
//string pat = #"("+textBox1.ToString()+")"; // not works
Someone have any idea? I try do it on diffrent ways and nothing works :(
for (int i = 0; i < listBox1.Items.Count; i++)
{
string text = listBox1.Items[i].ToString();
Regex r = new Regex(pat, RegexOptions.IgnoreCase);
Match m = r.Match(text);
}
Try textBox1.Text instead of textBox1.ToString()
string pat = "("+textBox1.Text+")";
or if you use C# 6.0 or above then
string pat = $"({textBox1.Text})";
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
ok I have no idea on how to do this and i have tried looking up how to do this but nothing good came up so ill ask it here. So what i am trying to do is:
string input = TextEditor.text; <-- this is in windows form application and
The "TextEditor" is the textbox for input
i want to take the string (which is input from the texct box) then split it so each word is on every other line like so:
if input = "hi my name is is"
out put should be:
hi: 1
my: 1
name: 1
is: 2 <-- if the word is said it shouldn't be repeated.
could someone please help me? I am a true newbie and i am completely lost. I don't have any code yet because I DONT KNOW HOW TO DO THIS!
Use Linq GroupBy and Count:
string inputText = "hi my name is is";
var words = inputText.Split(' ').ToList();
var wordGroups = words.GroupBy(w => w).Select(grp => new {
Word = grp.Key,
Count = grp.Count()
});
string outputText = string.Join("\n", wordGroups.Select(g => string.Format("{0}:\t{1}", g.Word, g.Count)));
/*
hi: 1
my: 1
name: 1
is: 2
*/
Split the input string into an array, then use that to build a dictionary. If the word is already in the dictionary, increment it. Otherwise add it with an initial value of 1.
string input = TextEditor.text;
string[] inputWords = input.Split(' ');
Dictionary<string, int> wordCounts = new Dictionary<string, int>();
foreach (string word in inputWords)
{
if (wordCounts.ContainsKey(word))
{
wordCounts[word]++;
}
else
{
wordCounts.Add(word, 1);
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to find the creation date of several .mp3 files, however wrong Dates are showing up, actually only one date is being repeated throughout the loop, I have done extensive research I really have however and I think it is a cache issue however I cant seem to make .Refresh work, I am using VS2010 my code is as follows
private static void Main()
{
var pstFileFolder = #"C:\Users\Damian\Downloads";
var searchPattern = "*.mp3";
var extension = ".mp3";
var serverFolder = #"C:\work\";
int count = 0;
foreach (var file in Directory.GetFiles(pstFileFolder, searchPattern))
{
string fileCreatedDatey = File.GetCreationTime(pstFileFolder).Date.ToString("yyyy-MM-dd");
var theefile = new FileInfo(file);
Console.WriteLine(fileCreatedDatey);
Console.WriteLine(theefile);
count++;
}
Console.WriteLine(count + searchPattern + " Files found");
Console.ReadKey();
}
File.GetCreationTime(pstFileFolder) will return you CreationDate for folder, and you will get the same value back for all files. Instead use:
string fileCreatedDatey = File.GetCreationTime(file).Date.ToString("yyyy-MM-dd");
Use this:
foreach (var file in Directory.GetFiles(pstFileFolder, searchPattern))
{
string fileCreatedDatey = File.GetCreationTime(file).Date.ToString("yyyy-MM-dd");
var theefile = new FileInfo(file);
Console.WriteLine(fileCreatedDatey);
Console.WriteLine(theefile);
count++;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I need to search through a string array to find every occurrence of "Parameters Table", and between each "Parameters Table" and the next, get another string from a specified index (that remains constant). I have been doing this like so:
public List<string> findlistOfNames(string[] arrayToLookForNames)
{
List<string> listOfNames = new List<string>();
const string separator = "Parameters Table"; //This is the string I am searching for
var cuttedWords = arrayToLookForNames.SkipWhile(x => x != separator).Skip(1);
while (cuttedWords.Any())
{
var variable = cuttedWords.TakeWhile(x => x != separator).ToArray();
cuttedWords = cuttedWords.Skip(variable.Length + 1);
listOfNames.Add(variable[2]); //This (always index 2) needs to be added to the list
}
return listOfNames;
}
This runs very slowly. Is there a better way to do this?
EDIT: Here is a snippet of string[] arrayToLookForNames:
Parameters Table
0
41
Baro Pressure
hPa
AFD2
recorded
Parameters Table
0
42
Baro Setting
in-hg
Seeing as you haven't specified what happens in the following case:
Parameters TableewfihweifhweParameters TableihwefwihewfParameters Table
where there are a total of 3 possible matches, I've chosen to assume that there's only one match per entry.
You could use regular expressions to state this somewhat more succinctly. Will probably be more efficient than your method too...
var regex = new Regex(#"(?<=Parameters Table).*?(?=(?:Parameters Table)|$)");
IEnumerable<string> foundValues =
arrayToLookForNames.SelectMany(x => regex.Matches(x))
.Where(m => m.Success)
.Select(m => m.Value);
... for the entry above, this would yield ewfihweifhwe
to cater for your better specified requirements:
var regex = new Regex(#"(?<=Parameters Table).*?(?=(?:Parameters Table)|$)",
RegexOptions.Singleline);
var vals = arrayToLookForNames
.SelectMany(x=>regex.Matches(x).Cast<Match>())
.Where(m=>m.Success)
.Select(m=>m.Value);