regex check for white space in middle of string - c#

I want to validate that the characters are alpha numeric:
Regex aNum = Regex("[a-z][A-Z][0-9]");
I want to add the option that there might be a white space, so it would be a two word expression:
Regex aNum = Regex("[a-z][A-Z][0-9]["\\s]");
but couldn't find the correct syntax.
id applicate any incite.

[A-Za-z0-9\s]{1,} should work for you. It matches any string which contains alphanumeric or whitespace characters and is at least one char long. If you accept underscores, too you shorten it to [\w\s]{1,}.
You should add ^ and $ to verify the whole string matches and not only a part of the string:
^[A-Za-z0-9\s]{1,}$ or ^[\w\s]{1,}$.

Exactly two words with single space:
Regex aNum = Regex("[a-zA-Z0-9]+[\s][a-zA-Z0-9]+");
OR any number of words having any number of spaces:
Regex aNum = Regex("[a-zA-Z0-9\s]");

"[A-Za-z0-9\s]*"
matches alphanumeric characters and whitespace. If you want a word that can contain whitespace but want to ensure it starts and ends with an alphanumeric character you could try
"[A-Za-z0-9][A-Za-z0-9\s]*[A-Za-z0-9]|[A-Za-z0-9]"

To not allow empty strings then
Regex.IsMatch(s ?? "",#"^[\w\s]+$");
and to allow empty strings
Regex.IsMatch(s ?? "",#"^[\w\s]*$");
I added the ?? "" as IsMatch does not accept null arguments

If you want to check for white space in middle of string you can use these patterns :
"(\w\s)+" : this must match a word with a white space at least.
"(\w\s)+$" : this must match a word with a white space at least and must finish with white space.
"[\w\s]+" : this match for word or white space or the two.

Related

Regex to replace specific control characters except a few special cases C#?

i have the following requirement:
i have a string str which has control characters...i want to replace these control characters with some specific values. So i am using the following Regex as:
str = Regex.Replace(str, #"\p{C}+","\r\n");
The above replaces ALL control characters with \r\n.
However, I want to do the same thing above but exclude the following control characters :
SPACE , `\u000D`, `\u000A`
How can i modify the RegEx above to accomplish this?
Any ideas? thanks!
Use a character class subtraction:
str = Regex.Replace(str, #"[\p{C}-[ \u000D\u000A]]+","\r\n");
^^^^^^^^^^^^^^^^^^^^^^^
The [\p{C}-[ \u000D\u000A]]+ pattern matches 1 or more chars from the \p{C} Unicode category except a space, \u000D and \u000A.
Here you go: [^\P{C}\r\n]+
Negative class [^
Negative property \P{C} (negative class + negative property = \p{C})
Carriage return \r
Line feed \n
Result: All control codes excluding CRLF.
(btw: SPACE is not matched by \p{C})

Replace all occurrences of a white space from string except the first occurrence using Regex in C#

The input string having multiple whitespaces, and the result should have only one white space left, other whitespaces must be replaced with string.Empty
Input String: +1 580 5691 234 or (435) 772-5992
Output String: +1 5805691234 or +1 4357725992
Regex.Replace(text, #"[^\d]", string.Empty) replaces all the whitespaces.
You don't have to do it all within one line:
Remove the special characters as you like, get the position of the first space, remove spaces with your regex only in the substring after the first space.
The .NET Regex engine can manage this with a lookbehind:
(?<=\s+.*)\s+
It matches all blocks of whitespace which are preceded by at least one block of whitespace followed by some non-whitespace characters.
Using it as Regex.Replace(text, "(?<=\s+.*)\s+", string.Empty) on the string +1 580 5691 234 produces the output +1 5805691234.
If you absolutely must do it with a single regex, this might be a solution for you:
Replace
^(?:\+1)?\D*(\d*)?\D*(\d*)?\D*(\d*)?\D*(\d*)?\D*(\d*)?$
with
+1 $1$2$3$4$5
Using 5 separate capture groups, it captures only digits, possibly separated by non-digits. If there are less than 5 digit groups, they're simply ignored.
Note that the optional preceding country code isn't captured either, and that the replace string always inserts it (back, if it existed).
This specific example limits the number of digit groups to 5, but could easily be expanded.
Here at regexstorm.net.
and illustrated here at regex101.

Regex to match trimmed string consisting of words separated by only 1 space char

I am looking for a regex to validate input in C#. The regex has to match an arbitrary number of words which are separated with only 1 space character in between. The matched string cannot start or end with whitespace characters (this is where my problem is).
Example: some sample input 123
What I've tried: /^(\S+[ ]{0,1})+$/gm this pattern almost does what is required but it also matches 1 trailing space.
Any ideas? Thanks.
I tried this one and it seems to work:
Regex regex = new Regex(#"^\S+([ ]{1}\S+)*$");
It checks if your string starts with a word followed by zero or more entities of a single white space followed by a word. So trailing white spaces are not allowed.

How to insert spaces between characters using Regex?

Trying to learn a little more about using Regex (Regular expressions). Using Microsoft's version of Regex in C# (VS 2010), how could I take a simple string like:
"Hello"
and change it to
"H e l l o"
This could be a string of any letter or symbol, capitals, lowercase, etc., and there are no other letters or symbols following or leading this word. (The string consists of only the one word).
(I have read the other posts, but I can't seem to grasp Regex. Please be kind :) ).
Thanks for any help with this. (an explanation would be most useful).
You could do this through regex only, no need for inbuilt c# functions.
Use the below regexes and then replace the matched boundaries with space.
(?<=.)(?!$)
DEMO
string result = Regex.Replace(yourString, #"(?<=.)(?!$)", " ");
Explanation:
(?<=.) Positive lookbehind asserts that the match must be preceded by a character.
(?!$) Negative lookahead which asserts that the match won't be followed by an end of the line anchor. So the boundaries next to all the characters would be matched but not the one which was next to the last character.
OR
You could also use word boundaries.
(?<!^)(\B|b)(?!$)
DEMO
string result = Regex.Replace(yourString, #"(?<!^)(\B|b)(?!$)", " ");
Explanation:
(?<!^) Negative lookbehind which asserts that the match won't be at the start.
(\B|\b) Matches the boundary which exists between two word characters and two non-word characters (\B) or match the boundary which exists between a word character and a non-word character (\b).
(?!$) Negative lookahead asserts that the match won't be followed by an end of the line anchor.
Regex.Replace("Hello", "(.)", "$1 ").TrimEnd();
Explanation
The dot character class matches every character of your string "Hello".
The paranthesis around the dot character are required so that we could refer to the captured character through the $n notation.
Each captured character is replaced by the replacement string. Our replacement string is "$1 " (notice the space at the end). Here $1 represents the first captured group in the input, therefore our replacement string will replace each character by that character plus one space.
This technique will add one space after the final character "o" as well, so we call TrimEnd() to remove that.
A demo can be seen here.
For the enthusiast, the same effect can be achieve through LINQ using this one-liner:
String.Join(" ", YourString.AsEnumerable())
or if you don't want to use the extension method:
String.Join(" ", YourString.ToCharArray())
It's very simple. To match any character use . dot and then replace with that character along with one extra space
Here parenthesis (...) are used for grouping that can be accessed by $index
Find what : "(.)"
Replace with "$1 "
DEMO

How to check if a string contains a pattern separated by whitespace?

How to check if a string contains a pattern separated by whitespace?
Examples:
"abc ef ds ab "
Now I would like to check if the given string consists only of the pattern [a-z] separated by whitespace. My try: ^\s*[a-z]*\s*$. But this checks only whitespace in the beginning and end, not if the whitespaces is used for separation of the content.
Try this regular expression:
/^[a-z\s]+$/
^(\s|[a-z])*$
Zero or more case characters that are either whitespace, or A-Z.
If you want to make sure there's at least one thing other than white space, then:
^\s*[a-z]+(\s*|[a-z])*$
Zero or more whitespace, at least one character A-Z, then the same as above.

Categories