How to get string to contain only numbers, dashes, and space? - c#

I am using regex to check if the string contains only numbers, dashes and spaces:
Regex regex = new Regex(#"^[0-9- ]+$");
if(!regex.IsMatch(str))
How can I do this without regex?

You can use linq to iterate over the characters, and char.IsDigit to check for a digit.
bool invalid = myString.Any( x => x != ' ' && x != '-' && !char.IsDigit(x) );

Here's a LINQ solution:
var allowedChars = "1234567890- ";
var str = "3275-235 23-325";
if (str.All(x => allowedChars.Contains(x))){
Console.WriteLine("true");
}

Related

Regex to split by a Targeted String up to a certain character

I have an LDAP Query I need to build the domain.
So, split by "DC=" up to a "comma"
INPUT:
LDAP://DC=SOMETHINGS,DC=ELSE,DC=NET\account
RESULT:
SOMETHING.ELSE.NET
You can do it pretty simple using DC=(\w*) regex pattern.
var str = #"LDAP://DC=SOMETHINGS,DC=ELSE,DC=NET\account";
var result = String.Join(".", Regex.Matches(str, #"DC=(\w*)")
.Cast<Match>()
.Select(m => m.Groups[1].Value));
Without Regex you can do:
string ldapStr = #"LDAP://DC=SOMETHINGS,DC=ELSE,DC=NET\account";
int startIndex = ldapStr.IndexOf("DC=");
int length = ldapStr.LastIndexOf("DC=") - startIndex;
string output = null;
if (startIndex >= 0 && length <= ldapStr.Length)
{
string domainComponentStr = ldapStr.Substring(startIndex, length);
output = String.Join(".",domainComponentStr.Split(new[] {"DC=", ","}, StringSplitOptions.RemoveEmptyEntries));
}
If you are always going to get the string in similar format than you can also do:
string ldapStr = #"LDAP://DC=SOMETHINGS,DC=ELSE,DC=NET\account";
var outputStr = String.Join(".", ldapStr.Split(new[] {"DC=", ",","\\"}, StringSplitOptions.RemoveEmptyEntries)
.Skip(1)
.Take(3));
And you will get:
outputStr = "SOMETHINGS.ELSE.NET"

Split alphanumeric string to array containing the alphabet and numeric characters separately

I'm looking to find a way to split an alphanumeric string like
"Foo123Bar"
into an array that contains it like so
array[0] = "Foo"
array[1] = "123"
array[2] = "Bar"
I'm not sure what the best way to achieve this is, especially because the strings I'm comparing follow no specific pattern as far as which is first, alphabet or numbers, or how many times they each appear. For example it could look like any of the following:
"Foo123Bar"
"123Bar"
"Foobar123"
"Foo123Bar2"
I'm trying to find out if there is a more efficient way of doing this other than splitting the string character by character and checking to see if it's numeric.
string input = "Foo123Bar";
var array = Regex.Matches(input, #"\D+|\d+")
.Cast<Match>()
.Select(m => m.Value)
.ToArray();
I dont think you will get around checking every character of the string.
You could try something like this:
string[] SplitMyString(string s)
{
if( s.Length == 0 )
return new string[0];
List<string> split = new List<string>(1);
split.Add("");
bool isNumeric = s[0] >= '0' && s[0] <= '9';
foreach(char c in s)
{
bool AddString = false;
if( c >= '0' && c <= '9' )
{
AddString = !isNumeric;
isNumeric = true;
}
else
{
AddString = isNumeric;
isNumeric = false;
}
if( AddString )
split.Add(c.ToString());
else
split[split.Count-1] += c;
}
return split.ToArray();
}

Retrieve String Containing Specific substring C#

I am having an output in string format like following :
"ABCDED 0000A1.txt PQRSNT 12345"
I want to retreieve substring(s) having .txt in above string. e.g. For above it should return 0000A1.txt.
Thanks
You can either split the string at whitespace boundaries like it's already been suggested or repeatedly match the same regex like this:
var input = "ABCDED 0000A1.txt PQRSNT 12345 THE.txt FOO";
var match = Regex.Match (input, #"\b([\w\d]+\.txt)\b");
while (match.Success) {
Console.WriteLine ("TEST: {0}", match.Value);
match = match.NextMatch ();
}
Split will work if it the spaces are the seperator. if you use oter seperators you can add as needed
string input = "ABCDED 0000A1.txt PQRSNT 12345";
string filename = input.Split(' ').FirstOrDefault(f => System.IO.Path.HasExtension(f));
filname = "0000A1.txt" and this will work for any extension
You may use c#, regex and pattern, match :)
Here is the code, plug it in try. Please comment.
string test = "afdkljfljalf dkfjd.txt lkjdfjdl";
string ffile = Regex.Match(test, #"\([a-z0-9])+.txt").Groups[1].Value;
Console.WriteLine(ffile);
Reference: regexp
I did something like this:
string subString = "";
char period = '.';
char[] chArString;
int iSubStrIndex = 0;
if (myString != null)
{
chArString = new char[myString.Length];
chArString = myString.ToCharArray();
for (int i = 0; i < myString.Length; i ++)
{
if (chArString[i] == period)
iSubStrIndex = i;
}
substring = myString.Substring(iSubStrIndex);
}
Hope that helps.
First split your string in array using
char[] whitespace = new char[] { ' ', '\t' };
string[] ssizes = myStr.Split(whitespace);
Then find .txt in array...
// Find first element starting with .txt.
//
string value1 = Array.Find(array1,
element => element.Contains(".txt", StringComparison.Ordinal));
Now your value1 will have the "0000A1.txt"
Happy coding.

Find NOT matching characters in a string with regex?

If Im able to check a string if there are invalid characters:
Regex r = new Regex("[^A-Z]$");
string myString = "SOMEString";
if (r.IsMatch(myString))
{
Console.WriteLine("invalid string!");
}
it is fine. But what I would like to print out every invalid character in this string? Like in the example SOMEString => invalid chars are t,r,i,n,g. Any ideas?
Use LINQ. Following will give you an array of 5 elements, not matching to the regex.
char[] myCharacterArray = myString.Where(c => r.IsMatch(c.ToString())).ToArray();
foreach (char c in myCharacterArray)
{
Console.WriteLine(c);
}
Output will be:
t
r
i
n
g
EDIT:
It looks like, you want to treat all lower case characters as invalid string. You may try:
char[] myCharacterArray2 = myString
.Where(c => ((int)c) >= 97 && ((int)c) <= 122)
.ToArray();
In your example the regex would succeed on one character since it's looking for the last character if it isn't uppercase, and your string has such a character.
The regex should be changed to Regex r = new Regex("[^A-Z]");.
(updated following #Chris's comments)
However, for your purpose the regex is actually what you want - just use Matches.
e.g.:
foreach (Match item in r.Matches(myString))
{
Console.WriteLine(item.ToString() + " is invalid");
}
Or, if you want one line:
foreach (Match item in r.Matches(myString))
{
str += item.ToString() + ", ";
}
Console.WriteLine(str + " are invalid");
Try with this:
char[] list = new char[5];
Regex r = new Regex("[^A-Z]*$");
string myString = "SOMEString";
foreach (Match match in r.Matches(myString))
{
list = match.Value.ToCharArray();
break;
}
string str = "invalid chars are ";
foreach (char ch in list)
{
str += ch + ", ";
}
Console.Write(str);
OUTPUT: invalid chars are t, r, i, n, g

Removing specific special characters from a string

I would like to remove spaces(' '), dots('.') and hyphens(-) from a string, using a regular expression.
My current approach:
string input = "hello how --r dsbadb...dasjidhdsa.dasbhdgsa--dasb";
var res = input
.ToCharArray()
.Where(i => i != ' ' && i != '-' && i != '.')
.Aggregate(" ", (a, b) => a + b);
string filteredInput = Regex.Replace(input, "[ .-]+", "");
should be easier and more readable.
var result = string.Concat(input.Where(c => !new[] { '.', ' ', '-' }.Contains(c)));
string result = Regex.Replace(input, "[\s\.-]+", "");
\s would target space, \. would target dots, and - would target hyphens and will replace them with empty string

Categories