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 5 3483
"=?Utf-8?B?RGlhbm5lIFN pZWJvbGQ=?=" <Dianne Si*****@discuss ions.microsoft. com> wrote in
news:47******** *************** ***********@mic rosoft.com: 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 (rgxIsValidChar s.IsMatch(testS tr))
{
// Only valid chars
}
else
{
// Some invalid chars
}
--
-mdb
mdb <m_b_r_a_y@c_t_ i_u_s_a__d0t__c om> wrote in
news:Xn******** *************** *****@207.46.24 8.16: if (rgxIsValidChar s.IsMatch(testS tr)) { // Only valid chars } else { // Some invalid chars }
OOOppsiee! I have this backwards... it should be
if (rgxIsValidChar s.IsMatch(testS tr))
{
// Some invalid chars
}
else
{
// Only valid chars
}
and really, if you wanna get really picky, the name of the Regex object
should be 'rgxHasInvalidC hars' or something along those lines, since that's
really what a positive match would indicate. Sorry for the confusion!!
--
-mdb
"=?Utf-8?B?RGlhbm5lIFN pZWJvbGQ=?=" <Dianne Si*****@discuss ions.microsoft. com> wrote in
news:47******** *************** ***********@mic rosoft.com: 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 rgxHasInvalidCh ars = new Regex("[^A-Za-z0-9_\-]");
if (rgxHasInvalidC hars.IsMatch(te stStr))
{
// Some invalid chars
}
else
{
// Only valid chars
}
--
-mdb
In article <47************ *************** *******@microso ft.com>,
Dianne Siebold <Dianne Si*****@discuss ions.microsoft. com> wrote:
: 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(st ring 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.WriteLi ne("input = [" + input + "]:");
string bad = FirstInvalid(in put);
if (bad == null)
Console.WriteLi ne(" valid!");
else
Console.WriteLi ne(" invalid: [" + bad + "]");
}
}
Output:
input = [???]:
invalid: [?]
input = [abc123]:
valid!
input = [I'm invalid]:
invalid: [']
Hope this helps,
Greg
Dianne Siebold wrote: 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
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mr.Clean |
last post by:
I am working on modifying a syntax highlighter written in
javascript and it uses several regexes. I need to add a
language to the avail highlighters and need the following regexes
modified to parse the new language, Delphi/Pascal. Source to the
highlighter is avail here:
http://www.dreamprojections.com/SyntaxHighlighter/Default.aspx
**********************************************
COMMENTS
|
by: William Stacey [MVP] |
last post by:
Would like help with a (I think) a common regex split example. Thanks for
your example in advance. Cheers!
Source Data Example:
one "two three" four
Optional, but would also like to ignore pairs of brackets like:
"one" <tab> "two three" ( four "five six" )
Want fields like:
|
by: Andrew Baker |
last post by:
I am trying to write a function which provides my users with a file
filter. The filter used to work just using the VB "Like" comparision,
but I can't find the equivilant in C#. I looked at RegEx.IsMatch but
it behaves quite differently. Is there a way I can mimic the DOS
filtering of filenames (eg. "*.*" or "*" returns all files, "*.xls"
returns all excel files, "workbook*" returns all files begining with
"workbook" etc)?
thanks in...
|
by: Mad Scientist Jr |
last post by:
i am trying to validate a field for a double, but not allow commas
the regex specifies
any number of whole number digits *
no comma *
an optional decimal point .?
and any number of digits after the decimal *
the whole thing looks like this:
|
by: lgbjr |
last post by:
Hi All,
I'm trying to split a string on every character. The string happens to be a
representation of a hex number. So, my regex expression is ().
Seems simple, but for some reason, I'm not getting the results I expect.
Dim SA as string()
Dim S as string
S="FBE"
| |
by: David Garamond |
last post by:
Is there a function like IS_VALID_REGEX() to check whether a pattern is
valid (i.e. it compiles)? I'm storing a list of regex patterns in a
table. It would be nice to be able to add a CHECK constraint to ensure
that all the regexes are valid.
If there isn't any, can I suggest Postgres add one? Although I know this
can probably be done in plpgsql using exception handling, or done in
plperl or plruby.
--
|
by: MattMika |
last post by:
Can anyone point out the problem with this? The commented regex var
and if statement dont work and break the GroupName check when
uncommented. I tested the AccessCodeRegxp with preg_match and it seems
to work fine, it just wont here. Any pointers would be great.
<script language="javascript"><!--
function check_form() {
var error = 0;
var error_message = "<?php echo JS_ERROR; ?>";
var customers_group_name =
|
by: PaulM |
last post by:
...all but the first x chars in a string of arbitrary length?
Apologies if this is the wrong forum; I wasn't sure the best place to post about Regex.
Background: I am new to Regex for pattern matching.
I have started using Yahoo Pipes to manipulate RSS feeds. Yahoo Pipes includes a regex gadget to do string manipulation, but no "stateful" programming.
My goal is to truncate a string of arbitrary length to a fixed length (of, say, the...
|
by: adamisko |
last post by:
Hello, what is wrong with this code, I want to divide string to pieces 4
chars length.
string tekst = "divide string to 4 chars pieces";
Regex regex = new Regex("(.{1,4})");
string substrings = regex.Split(tekst);
but instead of array with:
"divi","de s","trin" etc..
i have
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |