RegEx To Ensure Str Has Only Valid Chars

G

Guest

I'm not sure if I can use regex for this or how to do it, but here's what I
need to do: I want to check that a string contains only the characters A-Z,
a-z, 0-9, -(hypen) or _ (underscore).

If the string contains any character other than this, return a value. So if
a string returns an apostrophe, asterisk, percent sign, etc. it would return
the value.

TIA for any help!

Dianne Siebold
 
M

mdb

"=?Utf-8?B?RGlhbm5lIFNpZWJvbGQ=?=" <Dianne
(e-mail address removed)> wrote in
I'm not sure if I can use regex for this or how to do it, but here's
what I need to do: I want to check that a string contains only the
characters A-Z, a-z, 0-9, -(hypen) or _ (underscore).

Use the negated version of a character class expression...

Regex rgxIsValidChars = new Regex("[^A-Za-z0-9_\-]");

if (rgxIsValidChars.IsMatch(testStr))
{
// Only valid chars
}
else
{
// Some invalid chars
}
 
M

mdb

if (rgxIsValidChars.IsMatch(testStr))
{
// Only valid chars
}
else
{
// Some invalid chars
}

OOOppsiee! I have this backwards... it should be

if (rgxIsValidChars.IsMatch(testStr))
{
// Some invalid chars
}
else
{
// Only valid chars
}

and really, if you wanna get really picky, the name of the Regex object
should be 'rgxHasInvalidChars' or something along those lines, since that's
really what a positive match would indicate. Sorry for the confusion!!
 
M

mdb

"=?Utf-8?B?RGlhbm5lIFNpZWJvbGQ=?=" <Dianne
(e-mail address removed)> wrote in
I'm not sure if I can use regex for this or how to do it, but here's
what I need to do: I want to check that a string contains only the
characters A-Z, a-z, 0-9, -(hypen) or _ (underscore).

[This is a superseded message - the original was incorrect.]

Use the negated version of a character class expression...

Regex rgxHasInvalidChars = new Regex("[^A-Za-z0-9_\-]");

if (rgxHasInvalidChars.IsMatch(testStr))
{
// Some invalid chars
}
else
{
// Only valid chars
}
 
G

Greg Bacon

: I'm not sure if I can use regex for this or how to do it, but here's
: what I need to do: I want to check that a string contains only the
: characters A-Z, a-z, 0-9, -(hypen) or _ (underscore).
:
: If the string contains any character other than this, return a value.
: So if a string returns an apostrophe, asterisk, percent sign, etc. it
: would return the value.

Try code along the following lines:

static string FirstInvalid(string s)
{
Match m = new Regex(@"([^-_A-Za-z0-9])").Match(s);

if (m.Success)
return m.Groups[1].ToString();
else
return null;
}

[STAThread]
static void Main(string[] args)
{
string[] inputs = new string[]
{
"???",
"abc123",
"I'm invalid",
};

foreach (string input in inputs)
{
Console.WriteLine("input = [" + input + "]:");

string bad = FirstInvalid(input);
if (bad == null)
Console.WriteLine(" valid!");
else
Console.WriteLine(" invalid: [" + bad + "]");
}
}

Output:

input = [???]:
invalid: [?]
input = [abc123]:
valid!
input = [I'm invalid]:
invalid: [']

Hope this helps,
Greg
 
O

Oliver Sturm

Dianne said:
I'm not sure if I can use regex for this or how to do it, but here's what I
need to do: I want to check that a string contains only the characters A-Z,
a-z, 0-9, -(hypen) or _ (underscore).

If the string contains any character other than this, return a value. So if
a string returns an apostrophe, asterisk, percent sign, etc. it would return
the value.

The question you ask here has been sufficiently answered by others, but
I think this is not exactly the same question you were asking in the
subject. Often you actually need to ensure (!) that a string has only
the correct characters, while you're probably not much interested in the
exact mistakes the user may have made. In that case, a simple regex
based replacement is a good way to go. Like this:

string myString = "Wow, some text - I hope this works!";
string correctString = Regex.Replace(
myString, @"[^-A-Za-z0-9_], "");

Now you should have this in the correctString:

Wowsometext-Ihopethisworks

Hope this helps!



Oliver Sturm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top