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 3 years ago.
Improve this question
We are allowing the user to freely type into textbox like this:
SomeText[Foo.Id]-[Bar.Value] (valid)
Xyz[Foo.Id]_[Bar.Value] (valid)
abc-[pqr] (invalid)
Predefined values:
Foo.Id
Bar.Value
What is the best way to ensure that:
Texts inside [] should be matching to a predefined set of values
If any of the invalid text is entered, identify that wrong text
I think Regex would be the right way to go ahead with this.
A flexible way is to extract the text from the [] and verify it against a whitelist of your choice:
var validWords = new HashSet<string> {"[Foo.Id]", "[Bar.Value]"};
foreach (Match match in Regex.Matches("SomeText[Foo.Id]-[Bar.Value]-[Big.Mac]", #"(\[.*?\])")) {
foreach (Capture capture in match.Captures) {
if (!validWords.Contains(capture.Value)) {
Console.WriteLine($"{capture.Value} is not valid (Position {capture.Index})");
}
}
}
Prepare a string with all possible values. The string should look like this:
(?:possible_value1|possible_value2|...|possible_valueN)
Then use that inside this regex:
\w+\[REGEX_FOR_POSSIBLE_VALUES\][_-]\[REGEX_FOR_POSSIBLE_VALUES\]
For example, considering the only possible values are:
Foo.Id
Bar.Value
Then the final regex would be:
\w+\[(?:Foo.Id|Bar.Value)\][_-]\[(?:Foo.Id|Bar.Value)\]
This was considering you allways need two bracket groups.
If only one bracket group is possible, then use:
\w+\[REGEX_FOR_POSSIBLE_VALUES\](?:[_-]\[REGEX_FOR_POSSIBLE_VALUES\])?
If there can exists multiple bracket groups use:
\w+\[REGEX_FOR_POSSIBLE_VALUES\](?:[_-]\[REGEX_FOR_POSSIBLE_VALUES\])*
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 1 year ago.
Improve this question
For Example I have String like this:
""dear customer{Customer name} your reference number is {referenceNumber}"
I want to get array=["{Customer name}",{referenceNumber}]"
I have to split based on curly bracket inside bracket value is changeable means it can be different for different cases I just need to split and get array of value inside brackets including brackets.
If you think about it, splitting on { and } will produce an array where every odd index is what you want..
.Split('{','}').Where((s,i)=>i%2==1).Select(s=>'{' + s + '}').ToArray();
Split the string, use the LINQ Where function that passes the int index to the predicate, insist that the index be odd (mod2 is 1) and select a new string that puts the brackets back on, ToArray
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 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);
}
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
I got something like this:
string s="Solid;Solid;Gass;Solid;Solid;Gass;Solid;Gass;Liquid;Liquid;"
and now I want to get rid of the copies in the string...so that in the end s should be like this:
s="Solid;Gass;Liquid;"
Try this:
var parts = s.Split(';');
var distinctParts = parts.Distinct();
var newString = string.Join(";", distinctParts);
Where:
Split will give you an array with all the words of your string, taking the specified character as the word separator (; in this case).
Distinct will give your a collection with the unique words of your array.
Finally, Join composes a new string with the unique words, using the specified string (;in this case) as the separator.
You can split the string, then find the distinct instances and join them back in one line:
string s = "Solid;Solid;Gass;Solid;Solid;Gass;Solid;Gass;Liquid;Liquid;";
s = string.Join(";", s.Split(';').Distinct());
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
If the input string is
Cat fish bannedword bread bánnedword mouse bãnnedword
It should output
Cat fish bread mouse
What would be the best way to do this without slowing down the performance?
There are number of ways you can use but non of them (at least as far as I know) will work without certain performance cost.
The most obvious way is to remove the accented characters first and then use simple string.Replace(). As for removing accented characters this or this stackoverflow questions should help you.
Other approach could be splitting the string into an array of strings (each string being separate word) and then removing each word that equals the 'bannedword' using a parameter that makes Equals() method ignore accents.
Something like:
string[] splittedInput = input.Split(' ');
StringBuilder output = new StringBuilder();
foreach(string word in splittedInput)
{
if(string.Compare(word, bannedWord, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace) == false)
{
output.Append(word);
}
}
string s_output = output.ToString();
//I've not tested it in Visual Studio so there might be mistakes... (A LINQ could also simplify it (and potentially enable pluralization)).
And finally, it should be possible to come up with a clever regex solution (probably the fastest way) but not being an expert on regex I can't help you with that (this might point you in the right direction (if you know at least something about regexes)).