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})";
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
How can I extract this sub-string "60684" from this string "/fa/Viewer/Switcher/60684/0" in c#?
You can use Split method :
string str = "/fa/12012/Switcher/60684/0";
string str2 = str.Split('/')[4];
One way is to use regex:
static void Main()
{
string pattern = #"\/[a-zA-Z]+\/[a-zA-Z]+\/[a-zA-Z]+\/([0-9]+)\/[a-z0-9]+";
var regex = new Regex(pattern);
string path = "/fa/Viewer/Switcher/60684/0";
var match = regex.Match(path);
Console.WriteLine(match.Groups[1].ToString());
}
Like this:
string url = #"/fa/12012/Switcher/60684/0";
string[] NumberAfterSwitcher = url.Split('/');
string num = (NumberAfterSwitcher.Length > 3) ? (String)url.Split('/').GetValue(3) : String.Empty;
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:
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have files on a local server with the address of \\localServerAddress\Folder\Program.exe. I need to remove the server address dynamically and replace it with a different server address that is being selected elsewhere in the form. The server names can be different lengths, therefore, I can not use the string.Substring function.
So given the input
\\localServerAddress\Folder\Program.exe
I would like the result
\\differentServerAddress\Folder\Program.exe
If you are always working with UNCs
Then
string toRemove = new Uri(yourString).host;
string newString = yourString.Replace(String.format(#"\\{0})",toRemove)
, String.format(#"\\{0})",whateveryouwant));
Use this method:
string changeServerInPathString(string originalString, string newServer)
{
List<string> stringParts = originalString.TrimStart('\\').Split('\\').ToList();
stringParts.RemoveAt(0);
stringParts.Insert(0, newServer);
return string.Join("\\", stringParts.ToArray()).Insert(0, "\\\\");
}
You can use something like this:
void Main()
{
string path = #"\\localServerAddress\Folder\Program.exe";
UriBuilder bld = new UriBuilder(path);
bld.Host = "NewServer";
Console.WriteLine(bld.Uri.LocalPath);
}
Result: \\newserver\Folder\Program.exe
string text = #"\\test\FolderName\foo.exe";
text = text.Replace('\\', '-'); \\ this is done as I was not able to make the regex **\\\\\\(.)*?\\** , work.
Regex rg = new Regex("--.*?-"); \\ if in case the above mentioned regex is made to work correctly please replace the regex with the same.
text = rg.Replace(text, "");
Console.WriteLine(text.Replace('-', '\\'));
Console.Read();
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 needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Why doesn't this regex, constructed like this:
tmprect = string "gg_rct_MyReg1"
regex = #"^\s*set\s+"+tmprect+#"\s*=\s*Rect\s*\(\s*(.+)\s*,\s*(.+)\s*,\s*(.+)\s*,\s**(.+)\s*\).*$";
not work for
set gg_rct_MyReg1 = Rect (-704.0 , -352.0, 224.0 , 448.0) //rect 1
What did I do wrong?
///edited:
string findrectcoord = #"^\s*set\s+" + tmprect + #"\s*=\s*Rect\s*\(\s*([^,\s]*)\s*,\s*([^,\s]*)\s*,\s*([^,\s]*)\s*,\s*([^)\s]*)\s*\).*$";
StreamReader file3 = new StreamReader(openFileDialog1.FileName);
string line2;
while ((line2 = file3.ReadLine()) != null)
{
Regex foundrectr = new Regex(findrectcoord);
Match foundrectm = foundrectr.Match(line2);
if (foundrectm.Success)
{
MessageBox.Show("YES");
}
}
string:
set gg_rct_MyReg1 = Rect( -704.0 , -352.0, 224.0 , 288.0 ) //JassCode
Not Found
The regex itself, while ugly and inefficient, should work. You do need to assign the string you're adding into the regex before building the regex, though. While we're at it, let's clean it up:
string tmprect = "gg_rct_MyReg1";
Regex regexObj = new Regex(#"^\s*set\s+" + tmprect +
#"\s*=\s*Rect\s*\(\s*([^,\s]*)\s*,\s*([^,\s]*)\s*,\s*([^,\s]*)\s*,\s*([^)\s]*)\s*\).*$");
([^,\s]*) matches any number of characters except commas or spaces. That is more specific than .* which will match too much and force the regex engine to backtrack.