By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,990 Members | 1,406 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,990 IT Pros & Developers. It's quick & easy.

Regex Qs

P: n/a
tbh
hi, all,

a couple of questions re regular expressions in C#:
- is it possible to define new character classes? (e.g. \q to mean
something like \w | ['])
- is it possible to negate a character set *other* than with [^...] ?
(e.g. negate what I called \q above)

my motivation is, for example, to strip all non-legal "word" characters
including apostrophe.

i figured out a way to do it (getting, as it were, a list of positive
matches and splicing them together), but was curious whether there are more
elegant ways.

--Tim Hanson
May 12 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
No, you can't change the regular expression syntax.

As for more elegant ways, the question is, more elegant than what? You
haven't provided your exact requirements, or your solution. If you do, I
could look it over for you.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"tbh" <fe****@newsgroups.nospam> wrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
hi, all,

a couple of questions re regular expressions in C#:
- is it possible to define new character classes? (e.g. \q to mean
something like \w | ['])
- is it possible to negate a character set *other* than with [^...] ?
(e.g. negate what I called \q above)

my motivation is, for example, to strip all non-legal "word" characters
including apostrophe.

i figured out a way to do it (getting, as it were, a list of positive
matches and splicing them together), but was curious whether there are
more elegant ways.

--Tim Hanson

May 12 '06 #2

P: n/a
Yes, I agree with Kevin that you cannot change the syntax of RegEx. It is a
standard and is followed by all people.

Kevin Yu
Microsoft Online Community Support

================================================== ==========================
==========================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
================================================== ==========================
==========================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

May 15 '06 #3

P: n/a
What's wrong with using the expression : [^\w']

This code block

Regex regex = new Regex(@"[^\w']");
Console.WriteLine(regex.Replace("James's Curran's !@#$%^&*() widget's",
""));

prints : James'sCurran'swidget's

which appears to be what you requested.

May 15 '06 #4

P: n/a
Tim
that is what i wanted in this case, thanks! i guess i didn't realize
one could use "magic characters" denoting classes inside the square
brackets.

and it is more elegant than my work-around.

thanks for reading carefully and for the good suggestion.

--Tim

May 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.