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 6 years ago.
Improve this question
Good Afternoon,
I would like a regex to validate if the user typed two names or more and not only the first name, as in this example:
Ex: Fabrício (no match)
Ex:
Fabrício Oliveira (match)
Fabrício Oliveira Xavier (match)
Note: The expression must contain accents
Here's a Regex that only relies on a whitespace separator:
^\S+(\s\S+)+$
It makes these assumptions:
No name has a space, tab, or newline in it
Names are separated by exactly one space, tab, or newline
* this is based on #juharr's comment but with the parentheses to allow more than two names.
Edit: You can play around with this Regex here https://regex101.com/r/nS3hN8/1
Edit2: Added the beginning and ending anchors to the regex
Try this regular expression which matches two any length words separated by a space:
new Regex(#"\w+ \w+");
Really you could just use this:
if (Regex.Match(stringname, #"\w+\s\w+").Success)
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 3 years ago.
Improve this question
As the title indicates, how can I pass an array of delimiters to a Regex.Split function? I'm needing to split a sentence (string) by a list of words (also strings).
You can build a regex pattern from "delimiter" words like this:
var delim = new string[] {"fox", "lazy"};
var pattern = #"(?:\s|^)(?:" + string.Join("|", delim.Select(Regex.Escape)) + #")(?:\s|$)";
\s and string anchors at the beginning and at the end ensure that delimiters include all white space around them, and that you avoid the Scunthorpe problem. Using Regex.Espace ensures that delimiters with regex meta-characters do not break your code.
The resultant pattern looks as follows:
(?:\s|^)(?:fox|lazy)(?:\s|$)
Demo 1
If you would like to keep delimiter words among the tokens, change regex to use them in a lookahead/lookbehind:
var delimGroup = "(?:"+string.Join("|", delim.Select(Regex.Escape))+")";
var pattern = #"\s(?="+delimGroup+")|(?<="+delimGroup+#")\s";
Demo 2
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
I want to filter some bad words like 'asshole' but you can bypass the word by just saying 'asssssshole' or 'asshoooole'
Here is my code currently.
string Word = "asshole";
if (Comment.Contains(Word)
//block the comment from being posted
How would I check the message for multiple extra letters added on to a banned word, without creating hundreds of different rules for each way you can spell 'asshole'.
You might have some partial success using a soundex. Try putting your variations into https://www.functions-online.com/soundex.html and they all return the same value A240, even a*s*h*o*l*e. Unfortunately it won't work with a$$hole, but you might be able to come up with some simple substitutions to run before testing.
It should be pretty easy to find a c# implementation online.
Make a regex out of it and check for a match:
//using System.Text.RegularExpressions;
string word = "asshole";
//add a '+' after each letter to make it find any number of it
string pattern = string.Join("+", word.ToCharArray());
//pattern is "a+s+s+h+o+l+e"
if(Regex.IsMatch(comment, pattern))
{
//do something
}
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
I have this line:
abc cd_ef_1g fff
the 'abc' is a fixed value and always there. The fff can change.
I'm now searching for the following word, which is always second, always between whitespaces or tabs, and always after 'abc'.
cd_ef_1g
It could be with '_' or '-', or numbers.
I've searched all over, found similar results but not exactly with these conditions (using REGEX in C#).
Please help.
According to your description, this should be sufficient:
abc\s+([\w-]+)\s+fff
abc # Always before
\s+ # One or more white-space chars
([\w-]+) # One or more alphanumeric chars ('-' also included)
\s+ # One or more white-space chars
fff # Always after
The captured group is the word sought after.
Demo
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've got the following string all over my code:
languageService.getString("string_from_key");
Now I want to replace "string_from_key" (with the quotation marks) with KeyClass.string_from_key.
I am struggling with the RegEx in Visual Studio 2015 to search and replace the mentioned strings all over the code. string_from_key is a changing value, thats why RegEx. Thanks in advance.
Try matching more, and then using replacement parameters:
Search:
languageService.getString\("(\w+)"\);
Replace:
languageService.getString\(KeyClass.$1\);
Double quotes are not metachars in regexes, but parentheses are. \w+ will match more or more "word" characters, but that includes underscores (because history) so is very useful for identifiers.
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 6 years ago.
Improve this question
the cases are
compulsory '\' char at the first
followed by alphanumeric
compulsory '\' char at the last
eg:\abc\bvc\
\abc4\abc3\abc2\abc1\
Use ^ to match the begging of the string and $ to match the end of the string. Then use character class with word character.
The following allows only the alphanumeric characters
^\\[\w\\]+\\$
and then following allows any character.
^\\.+\\$
Try this:
^\\(.+\\)+$
This requires \ at the beginning and end. Also can have multiple words for a single folder name. Also allows one or more folders.
Of course there are many more valid folder paths that don't meet this regex. This regex allows characters that are not valid for folder names; not sure what all the limitations are for folder names. See this question for a discussion on valid folder names.