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 7 years ago.
Improve this question
Help write a regular expression to search for files and folders,
searches for a given mask. In the mask, you can use "*"
(any characters in any number), and the "?" (one symbol).
Here shows you how to use regex in C#.
You could always just loop the directory that you're looking in and check the file names instead of making a regex. (You'll need to use System.IO)
Perhaps something like this?
string [] fileEntries = Directory.GetFiles(targetDirectory);
Regex regex = new Regex("target file name");
Match match = regex.Match(string.Join(" ", fileEntries););
if (match.Success)
{
Console.WriteLine(match.Value);
}
Related
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 2 years ago.
Improve this question
I want to create a regular expression to check the following string pattern, I tried following these tutorials but it's still confusing. Any help is appreciated.
Type: In Folder
(T or t)ype(spaces or no space):(spaces or no space)(i or I)n(spaces or multiple space)(f or F)older
So following your desired pattern at the end of your question:
(t|T)ype\s*:\s*(i|I)n\s*(f|F)older
The above pattern should match your string. Mind you, \s* is zero to unlimited spaces, which means it would match on the string you provided even if there were no spaces present; if there will always be at least one space present, you can replace them with \s+
Hope this helps!
(T|t)ype\s*:\s*(I|i)n\s*(f|F)older
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 6 years ago.
Improve this question
I am trying to concat a variable to a regen in c# but it is not working
string color_id = "sdsdssd";
Match variations = Regex.Match (data, #""+color_id+"_[^\""]*\""\W\,\""sizes\""\:\s*\W.*?businessCatalogItemId"":\"")", RegexOptions.IgnoreCase);#""+color_id+"_[^\""]*\""\W\,\""sizes\""\:\s*\W.*?businessCatalogItemId"":\"")";
But the above is not working
How to concat a variable at starting element to regex in c#
The # identifier only affects the immediately following literal string - you need to apply it to each string that needs it:
Match variations = Regex.Match (data,color_id +
#"_[^\""]*\""\W\,\""sizes\""\:\s*\W.*?businessCatalogItemId"":\"")",
RegexOptions.IgnoreCase);
Your code is not working because you appear to have put this into your code twice.
#""+color_id+"_[^\""]\""\W\,\""sizes\"":\s\W.*?businessCatalogItemId"":\"")"
Removing that should allow the concatenation to work.
Alternatively, you could use String.Format to make the pattern
string pattern = String.Format("#{0}_[^\""]*\""\W\,\""sizes\""\:\s*\W.*?businessCatalogItemId"":\"")", color_id)
Match variations = Regex.Match (data, pattern, RegexOptions.IgnoreCase);
In the String.Format, it will replace the {0} with color_id. You can use this to insert multiple variable into the string. Take a look at this MSDN page for more info
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 6 years ago.
Improve this question
I would like to exclude a specific string when it is contained in an expression:
Example:
myurl.htm = exclude
myurl = include
I tried this one : ([a-z0-9]+)(?!.htm)
But looks like it doesn't work.
Try the following:
([a-z0-9]+)(?!^\.htm)
You had two errors in your expression:
You have to escape the dot . with a backslash because it means "matches any character (except newline)" unescaped.
You have to add a ^ to prevent cutting of the last character.
You can test your expression on this website: https://regex101.com/
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 8 years ago.
Improve this question
I have a large text. I need find any word in the text then copy to variable the phrase that contains the word. How can I do this by C#? Maybe I can use some regular expression?
Use the regex expression [^.!?;]*(search)[^.?!;]*[.?!;], where "search" is your query.
string query = "professional";
var regex = new Regex(string.Format("[^.!?;]*({0})[^.?!;]*[.?!;]", query));
string text =
#"Stack Overflow is a question and answer site for professional and enthusiast programmers.
It's built and run by you as part of the Stack Exchange network of Q&A sites.
With your help, we're working together to build a library of detailed answers to
every question about programming.";
var results = regex.Matches(text);
for (int i = 0; i < results.Count; i++)
Console.WriteLine(results[i].Value.Trim());
This code uses the regex to find all sentences containing "professional", and outputs them, trimming any whitespace.
Output: Stack Overflow is a question and answer site for professional and enthusiast programmers.
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 8 years ago.
Improve this question
I have 3 strings from wich I want to extract the movie title, if posible in one RegularExpression
<title>Airplane! (1980)</title>
<title>"24" (2001)</title>
<title>"Agents of S.H.I.E.L.D." The Magical Place (2014)</title>
My best shot so far is this one:
<title>(")?(.*?)(")?.*?\((\d{4})\).*?</title>
Works fine for "Agents of S.H.I.E.L.D." and "24" but not for "Airplane!".
What am I doing wrong?
Even though it might not be clear the regular expression are called within a C# program, and I'm using RegEx
RE for start-of-line => opening tag => optional " => read until " or (nnnn)
titles = System.Net.WebUtility.HtmlDecode(titles);
foreach (Match match in Regex.Matches(titles,
#"^\s*<title>\s*\""*(.*?)(\""|\(\d{4}\))", RegexOptions.Multiline | RegexOptions.IgnoreCase))
{
if (match.Success)
{
string name = match.Groups[1].Value;
}
}